A
A
Andrey Kolechko2019-07-01 17:24:23
Django
Andrey Kolechko, 2019-07-01 17:24:23

Multi-linked list of models in Django?

5d1a163696a20469870550.png
In general, you need to make something like this structure of models. Each model has two fields - the previous elements and the next (each field can contain several objects). It suggests itself to use ManyToMany, but for some reason it does not work out. The code was written like this:

class Post(models.Model):
    title = models.CharField(max_lenth=50)
    next_posts = models.ManyToManyField('self', related_name='prev_posts', blank=True)

If you have any ideas, or maybe someone came across, please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Kolechko, 2019-07-01
@AnkoII

Once again, I answer to myself (lul can help someone later).
It turned out that it was necessary to add the symmetrical=False parameter (and it also turned out that it was better to search in English):

class Post(models.Model):
    title = models.CharField(max_lenth=50)
    next_posts = models.ManyToManyField('self', related_name='prev_posts', symmetrical=False, blank=True)

Now it works as it should

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question