Django, queryset to bring back manytomany from himself

Following the regular multipoint query logic, I get errors. I suppose that, since it is connected with itself, I may need to do additional magic, but I cannot understand what kind of magic it is.

Model:

class Entry(models.Model):
    title   =   models.CharField(max_length=100, verbose_name='title')
    related     =   models.ManyToManyField('self', related_name='related_entries', blank=True)

QuerySet:

entry = Entry.objects.get(pk=1)
related = entry.related_entries.all()

error: Entry object does not have attribute related_entries

+3
source share
1 answer

See the documentation for an symmetricalargument ManyToManyField:

Django , , ManyToManyField , person_set Person. ManyToManyField - , , .

, , False. Django , ManyToManyField .

, related_name, symmetrical=False.

+8

Source: https://habr.com/ru/post/1769968/


All Articles