Django how to deal with foreign keys in child tables

I am new in DJango. I have a database with three tables:

> -CRP
>  - SubComponents of a CRP
>     - Objectives of a SubComponent of a CRP.

I have the following models:

class period(models.Model):
    period_code = models.IntegerField(primary_key=True,verbose_name='Period code')
    period_desc = models.CharField(max_length=45,verbose_name='Period description')
    period_current = models.IntegerField(verbose_name='Current period')
    def __unicode__(self):
        return self.period_desc
    class Meta:
        db_table = 'period'

class crp(models.Model):
    crp_code = models.CharField(max_length=3,primary_key=True,verbose_name='CRP code')
    crp_desc = models.CharField(max_length=45,verbose_name='CRP description')
    def __unicode__(self):
        return self.crp_desc
    class Meta:
        db_table = 'crp'

class subcomponent(models.Model):
    crp_code = models.ForeignKey(crp,db_column='crp_code',related_name='subcomponent_crp_code',to_field='crp_code',primary_key=True,verbose_name='CRP code')
    subc_code = models.CharField(max_length=3,primary_key=True,verbose_name='SubComponent Code')
    subc_desc = models.CharField(max_length=45,verbose_name='SubComponent Description')
    def __unicode__(self):
        return self.subc_desc
    class Meta:
        db_table = 'subcomponent'

class objective(models.Model):
    crp_code = models.ForeignKey(subcomponent,db_column='crp_code',related_name='objective_crp_code',to_field='crp_code',primary_key=True,verbose_name='CRP code')
    subc_code = models.ForeignKey(subcomponent,db_column='subc_code',related_name='objective_subc_code',to_field='subc_code',primary_key=True,verbose_name='SubComponent Code')
    obj_year = models.ForeignKey(period,db_column='obj_year',related_name='objective_obj_year',to_field='period_code',primary_key=True,verbose_name='Objective year')
    obj_code = models.CharField(max_length=7,primary_key=True,verbose_name='Objective code')
    obj_desc = models.CharField(max_length=45,verbose_name='Objective description')
    def __unicode__(self):
        return self.obj_desc
    class Meta:
        db_table = 'objective'

All works fine for the "subcomponent" model (reference to CRP); in the administration of "subcomponent" the user can pull down a CRP and add a subcomponent to it.

However, the model "objective" reference the model "subcomponent". In the administration of "objective" I would like the user to pull down and select a CRP, then filter the subcomponents of that CRP also in a pull down list. How can I do this?

Many thanks, Carlos.


Take a look on django-smart-selects

Need Your Help

Best way to represent 3D objects in javascript

javascript data-structures 3d

I want to represent 3D objects in javascript. Actually, I want to represent objects in a video (which would be x,y, and time, so it's like a 3D object). I will have a set of objects which are defin...

How can I generate a .torrent in Java?

java api bittorrent

I want to generate a .torrent file in Java, but I don't want a big API that does anything like scraping trackers, seeding, etc. This is just for a client that generates meta data. What lightweight

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.