I have a simple model like this one:
class Artist(models.Model):
surname = models.CharField(max_length=200)
name = models.CharField(max_length=200, blank=True)
slug = models.SlugField(unique=True)
photo = models.ImageField(upload_to='artists', blank=True)
bio = models.TextField(blank=True)
class Images(models.Model):
title = models.CharField(max_length=200)
artist = models.ManyToManyField(Artist)
img = models.ImageField(upload_to='images')
Well, I started to insert some Artists and then I went to the Images insert form. I found that the many-to-many artist box is unsorted:
Instead of:
How can I solve this issue? Any suggestion? Thank you in advance.
Matteo
When you add a calculated field Django doesn't know how to do a order_by , so it doesn't add sorting capability on that field. If you want to add sorting on a calculated field, you have to tell Django what to pass to order_by . You can do this by setting the admin_order_field attribute on the calculated field method.
The Django admin is an automatically-generated user interface for Django models. The register function is used to add models to the Django admin so that data for those models can be created, deleted, updated and queried through the user interface.
Set ordering
on the Article's inner Meta
class.
class Article(models.Model):
....
class Meta:
ordering = ['surname', 'name']
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