【问题标题】:how to filter based on created an object for froingkey django如何根据为froingkey django创建的对象进行过滤
【发布时间】:2021-09-19 13:56:40
【问题描述】:

我有两个模型(表)TicketingCancel

class Ticketing(models.Model):
    a= models.CharField(max_length=40)
    b = models.DecimalField(max_digits=10,decimal_places=2)
    #others

class Cancel(models.Model):
    ticket = models.ForeignKey(Ticketing,on_delete=models.CASCADE,related_name="ticket")
    #others

views.py ,我必须进行查询以显示所有活动的Ticketing

Ticketing.objects.filter()#how to filter all objects which `cancel` not created for

谢谢,我知道创建一个布尔字段,然后在 Ticketing 中取消创建 BooleanField 时将为 False,但我不想使用它,可以吗?!

【问题讨论】:

    标签: python django filter


    【解决方案1】:

    您可以使用查找 isnull 过滤活动的票务实例。

    Ticketing.objects.filter(ticket__isnull=True)
    

    【讨论】:

    • 这个解决方案有效吗?票务@HunarMohammed 中没有名为ticket 的字段
    • tickting 字段在Cancel 表中
    • @umair 这是从 Cencel 到 Ticketing 的反向连接
    • 是的,但是您在此处查询票务模型。那里有related_name 之类的吗?
    • 您可以使用related_name 作为从相关对象回到这个对象的关系的名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多