Skip to content


A biophysicist teaches himself how to code

I still don’t claim to understand inheritance well (although it should be simple), but I think that part of my trouble was stemming from differences between Django and Python. Regardless, I managed to solve my problem from the last post with something like this:

class Protein(models.Model):
class Domain(models.Model):
    protein = models.ForeignKey(Protein)
    def domain_percent(self):
        m = re.match('^RING', self.domain)
        if m:
            p = Protein.objects.get(pk=self.protein_id)
            p_clean = re.sub("\s+", "", p.sequence)
            p_len = float(len(p_clean))
            domain_l = float(self.domain_stop - self.domain_start)
            perc = int(100*(domain_l/p_len))
            return perc

the key line is

p = Protein.objects.get(pk=self.protein_id)

. This says “get all of the objects from the related class “Protein” which have a primary key (pk) equal to the protein_id of this domain. Whew.

As usual, the key information I needed was in the part of the tutorial that I skimmed. I’ve spent most of the day today struggling with querysets, but I’m still a long way from maximizing them as well. I’ve managed to get things working “well enough” for the time being – I need to stop tinkering with the backend of this for now and actually populate/extract data if I want to be able to wow my advisor at our individual meeting tomorrow.


Tags: , ,

%d bloggers like this: