Reverse self-translating foreign key in Django

Sorry if this is a stupid question, but I'm a bit new to Django and can't seem to find an answer.

I have an order model with a self-regulation field:

source = models.ForeignKey(
    'self',
    help_text = "[redacted]",
    verbose_name = "Source Order",
    blank = True,
    null = True,
)

      

While this works, if I have an order and I want to know its source, I also need to get a list of the "children" of the order, that is, a list of orders for which this order is the source. Does this need to be done via filter()

or is there a good Django-y way to do this?

+3


source to share


1 answer


If you have an Order object, you can use the inverse relationship :

child_orders = my_order.order_set.all()

      



You can provide a field with a source

more descriptive value related_name

to be used instead of the order_set

above.

+5


source







All Articles