8

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:

  • Mondino Aldo
  • Aliprandi Bernardo
  • Rotella Mimmo
  • Corpora Antonio

Instead of:

  • Aliprandi Bernardo
  • Corpora Antonio
  • Mondino Aldo
  • Rotella Mimmo

How can I solve this issue? Any suggestion? Thank you in advance.

Matteo

Matteo
  • 1,217
  • 1
  • 13
  • 17

1 Answers1

8

Set ordering on the Article's inner Meta class.

class Article(models.Model):
    ....

    class Meta:
        ordering = ['surname', 'name']
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Don't! This has as performance implications for your whole app (see the warning in the linked django doc). I would recommend using Daniel's better answer here: http://stackoverflow.com/a/1474175/758345 – Chronial Feb 08 '16 at 09:58