We have been struggling with this for a few days and have done lots of searches on the web.
We are trying to figure out how entries are saved in Django forms for many to many fields.
For example we have a news model that has a many to many relationship with images. When we add images to a news article e.g. images with id 10,2,14 we can see the post values are the following when saving a news article form:
When we look in the many to many intersection table the order has not been preserved. We can see no logic in the order the photos are inserted.
Hopefully that makes sense!
Many thanks for your answers in advance.
Arif
Django doesn't guarantee to preserve the order of many-to-many relationships. If you need this, you should use a through
table with an explicit 'order' field.
Thankfully, order can now be preserved in a ManyToMany relationship without the extra complexity of using a through model using sortedm2m which is a drop-in replacement for django's own ManyToManyField.
https://github.com/gregmuellegger/django-sortedm2m
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With