Opened 4 years ago

Closed 4 years ago

#28899 closed Uncategorized (needsinfo)

recursive relationship only works one way

Reported by: HenrySiau Owned by: nobody
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords: ForeignKey, self
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


class Catalog(models.Model):
    parent = models.ForeignKey('self',related_name='children', on_delete=models.CASCADE, null=True, blank=True)

Catalog.parent always return null, while Catalog.children.all() works fine.

Change History (1)

comment:1 Changed 4 years ago by Sergey Fedoseev

Resolution: needsinfo
Status: newclosed

I tried this and it worked for me:

In [1]: from test_app.models import Catalog

In [2]: parent = Catalog.objects.create()

In [3]: child = Catalog.objects.create(parent=parent)

In [4]: child.parent
Out[4]: <Catalog: Catalog object (1)>

In [5]: child.refresh_from_db()

In [6]: child.parent
Out[6]: <Catalog: Catalog object (1)>
Note: See TracTickets for help on using tickets.
Back to Top