Sorry if this is a stupid question, but I'm a bit of a Django newbie and can't find the answer.
对不起,如果这是一个愚蠢的问题,但我有点像Django新手,无法找到答案。
I have an Order model with a self-referencing field:
我有一个带有自引用字段的Order模型:
source = models.ForeignKey(
'self',
help_text = "[redacted]",
verbose_name = "Source Order",
blank = True,
null = True,
)
While this works fine if I have an Order and I want to know its source, I also need to get a list of an Order's "children" - that is, a list of Orders for which this Order is the source. Does this need to be done through filter()
s or is there a nice, Django-y way to do it?
虽然如果我有一个订单并且我想知道它的来源,这可以正常工作,我还需要获得一个订单的“子”列表 - 也就是这个订单作为源的订单列表。这需要通过filter()来完成还是有一个很好的Django-y方法呢?
1 个解决方案
#1
5
If you have an Order object, you can use the reverse relationship:
如果您有Order对象,则可以使用反向关系:
child_orders = my_order.order_set.all()
You may want to give the source
field a more descriptive related_name
value which would be used in place of order_set
above.
您可能希望为源字段指定一个更具描述性的related_name值,该值将用于代替上面的order_set。
#1
5
If you have an Order object, you can use the reverse relationship:
如果您有Order对象,则可以使用反向关系:
child_orders = my_order.order_set.all()
You may want to give the source
field a more descriptive related_name
value which would be used in place of order_set
above.
您可能希望为源字段指定一个更具描述性的related_name值,该值将用于代替上面的order_set。