I am trying to change the order of the form field in the table when the user sorts it in the dropdown menu. Basically, it will be in the uppermost left of the table when the user sorted it
the original table:
the table will look like this when the user sorted it in name
field:
in my forms.py
i have the field in order of:
class myForm(forms.ModelForm):
class Meta:
model = myModel
fields = [
"email",
"name",
"order",
]
and in my models.py
:
class myModel(models.Model):
name = models.CharField(max_length=100, null=True)
email = models.CharField(max_length=100, null=True)
role = models.CharField(max_length=100, null=True)
my template.html:
{%for items in qs %}
<tr>
<td>
<a href="{% url 'crud-view' items.id %}">View</a>
|
<a href="{% url 'crud-update' items.id %}">Edit</a>
|
<a href="{% url 'crud-delete' items.id%}">Delete</a>
</td>
<td>{{items.email}}</td>
<td>{{items.name}}</td>
<td>{{items.role}}</td>
in my views.py
i tried:
qs = myModel.objects.all().order_by("name")
but it is just sorting the items inside the name
field. how can I make it so that the name
field will appear at the uppermost left part of the table?
CodePudding user response:
You can use order_fields
method to achieve that.
self.order_fields(sorted_fields)
Documentation: https://docs.djangoproject.com/en/3.2/ref/forms/api/#django.forms.Form.order_fields
CodePudding user response:
I solved it using django-django-tables2-column-shifter