Justin Andrew
Justin Andrew

Reputation: 242

Filter ForeignKey in Django Model

class Wod(models.Model):
    title = models.CharField(max_length=50)
    workout = models.CharField(max_length=250)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self): 
        return self.title
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

class Score_Choice(models.Model):
    wod = models.ForeignKey(Wod)
    choice_text = models.CharField(max_length=200)
    def __unicode__(self): 
        return self.choice_text

class Score(models.Model):
    wod = models.ForeignKey(Wod)
    user = models.ForeignKey(User)
    performed_date = models.DateTimeField('date performed')
    **type = Score_Choice.objects.filter(Score_Choice__wod = 'wod')** 

    score = models.IntegerField()

I have a model with Wods, which is referenced by Score_Choice to assign different kinds of possible scores to each Wod.

When recording the score, I wish to be able to reference the available kinds of scores available for that wod.

The desired functionality is such that when entering a score I select the wod I will be entering score(s) for, then I will have available the different kinds of scores needed for that wod.

Upvotes: 1

Views: 190

Answers (1)

Aljana Polanc
Aljana Polanc

Reputation: 950

I guess you send an id of a selected wod via ajax or something, and then you need a query to read associated score choices. The key is in using suffix "_id".

Score_Choice.objects.filter(wod_id=123)

Upvotes: 1

Related Questions