简体   繁体   English

在Django模型中过滤ForeignKey

[英]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. 我有一个带有Wods的模型,Score_Choice引用了该模型,以便为每个Wod分配不同类型的可能分数。

When recording the score, I wish to be able to reference the available kinds of scores available for that wod. 记录乐谱时,我希望能够引用可用于该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. 所需的功能是,当输入乐谱时,我选择要输入乐谱的wod,那么我将可获得该wod所需的各种乐谱。

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. 我猜您是通过ajax或其他方式发送选定wod的ID,然后需要查询才能读取关联的乐谱选择。 The key is in using suffix "_id". 关键是使用后缀“ _id”。

Score_Choice.objects.filter(wod_id=123) Score_Choice.objects.filter(wod_id = 123)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM