Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7024471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:50:38+00:00 2026-05-27T23:50:38+00:00

I’m writing a scientific web app in Django dealing with the amino acid sequences

  • 0

I’m writing a scientific web app in Django dealing with the amino acid sequences of antibodies Fab fragments, each of which is comprised of exactly one Heavy Chain and one Light Chain. Each of these chains consists of a sequence of amino acid Residues.

  • Fab 1
    • Light Chain
      • Residue 1
      • Residue 2
      • …
    • Heavy Chain
      • Residue 1
      • Residue 2
      • …
  • Fab 2
    • etc…

My models.py is essentially this:

from django.db.models import *

class Fab(Model):
    name = CharField(max_length=30)
    ...
    def __unicode__(self):
        return self.name

class Chain(Model):
    fab = ForeignKey(Fab)
    TYPE_CHOICES = (
        ('L', 'light'),
        ('H', 'heavy'),
    )
    type = CharField(max_length=5)
    ...

class Residue(Model):
    ch = ForeignKey(Chain)
    ...

So in the process of entering an Fab into the database, I create 2 chains, assign each a type and an fab foreign key. Then, to use these in a template, I use the following view, getting each chain as an object and passing it to the template independent of its Fab parent object, which isn’t exactly ideal.

def fab_detail(request, fab_id):

    f = get_object_or_404(Fab, pk=fab_id)
    h = get_object_or_404(Chain, fab=f, type='H')
    l = get_object_or_404(Chain, fab=f, type='L')

    return render_to_response('antibodies/fab_detail.html', {
        'fab': f,
        'light': l,
        'heavy': h,
    }, context_instance=RequestContext(request))

However, I want to:

  1. have a better way to refer to the Light or Heavy Chain in a template, e.g. to loop over the residues of the chain with {% for r in fab.light_chain.residue_set.all %}.
  2. ensure that each Fab has only 1 light chain and 1 heavy chain

I’ve considered subclassing Chain but wasn’t sure exactly how to achieve a similar result. I came up with something along the lines of:

class Chain(Model):
    # same as before, but without the fab ForeignKey field
    ...

class LightChain(Chain):
    pass

class HeavyChain(Chain):
    pass

class Fab(Model):
    name = CharField(max_length=30)
    light_chain = OneToOneField(LightChain)
    heavy_chain = OneToOneField(HeavyChain)
    ...

class Residue(Model):
    ???

The main problem I’m having is how to get the LightChain and HeavyChain fields to contain Residue data. Specifically, with what do I replace ch = ForeignKey(Chain) in the Residue class?

Any suggestions or references will be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:50:38+00:00Added an answer on May 27, 2026 at 11:50 pm

    keni’s solution is the one I was about to write.

    However, I don’t think that the “choices=TYPE_CHOICES” constraint is enforced at any level, it just tells Django to use a “select” menu in forms and admin. So theoretically you could have type = ‘R’, ‘W’ or anything. Btw, I think you (jared) meant max_length=1.

    Another solution would be to simply use a multi-table inheritance, as you seem to do, and not an abstract base class, which are two different forms of model inheritance. In which case you can simply have ch = ForeignKey(Chain). But that may be too much overhead: three tables would be created, one for Chain, one for Light and one for Heavy, the latter two ones referencing the first, one and containing basically nothing else. It may be interesting if you need to store specific information for Light or Heavy chains.

    A third solution would be to do this:

    class Fab(Model):
    name = CharField(max_length=30)
    light = OneToOneField(Chain, related_name="fab_as_light")
    heavy = OneToOneField(Chain, related_name="fab_as_heavy")
    

    This way you can do fab.light and fab.heavy very easily, and uniqueness is enforced. I’m pretty sure it’s legal to have two OneToOneField towards the same model. If it’s not you can still have a Foreign Key and set it “unique”.
    I think the third one is your solution.

    For completeness, you’d have:

    class Residue(Model):
    ch = ForeignKey(Chain)
    

    And Chain would be almost empty (just the id).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.