Reputation: 11
I have a Django model like this (hypothetical):
class Person(models.Model):
name = models.CharField()
last_name = models.CharField()
age = models.IntegerField()
I access this with an API call so I get the json {name: "John", last_name: "Doe", age: 20}
and show this as an ordered list like this
I want to reorder this list by dragging the elements (that's easy with this) but once saved and reloaded the data returns to original order because the order isn't saved. Is there a way to achieve this? The best I can think of is getting a new field fields_order
that keeps the fields names with the specified order but I think that should be a better approach.
Upvotes: 1
Views: 97
Reputation: 836
we handle this issue by defining an index field with an integer type that is like a point for any object, after that with use of Meta class in model class, the object's ordered with this field
Class UModel(models.Model):
.
.
index = models.IntegerField()
class Meta:
ordering = ('-index',)
Upvotes: 1