0

I've already read a lot of questions related to this problem, like How to custom sort a Django queryset but no answer worked.

Here's are my models:

class Groupe(models.Model):
    parents = models.ManyToManyField('self', blank=True,
                                     through='GroupeRelation',
                                     symmetrical=False,
                                     related_name='parent')
    description = models.CharField(max_length=50)

class GroupeRelation(models.Model):
    groupe_parent = models.ForeignKey(Groupe,
                                      related_name='groupe_parent',
                                      verbose_name=_(u'Parent'))
    groupe_enfant = models.ForeignKey(Groupe,
                                      related_name='groupe_enfant',
                                      verbose_name=_(u'Enfant'))

class Mot(models.Model):
    groupes = models.ManyToManyField(Groupe)
    texte = models.CharField(max_length=50)

    def mot_avec_groupe(self):
        return str(self)
    mot_avec_groupe.short_description = _(u'Description')
    mot_avec_groupe.allow_tags = True

    def __str__(self):
        return _(u'{0} / {1}').format(
            u''.join([g.description for g in self.groupes.all()]),
            self.texte
        )

And I'm trying to display a sortable column in the admin of Mot like this:

class MotGroupesInline(admin.TabularInline):
    model = Mot.groupes.through
    extra = 0

class MotAdmin(admin.ModelAdmin):
    readonly_fields = ('mot_avec_groupe', )
    fields = ('texte', )
    inlines = (MotGroupesInline,)
    list_display = ('mot_avec_groupe', 'texte')

It's properly displayed, but I can't sort by mot_avec_groupe column. What am I missing?

Community
  • 1
  • 1
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • 2
    `mot_avec_groupe` is not a data base field. See here for sorting by property or function: http://stackoverflow.com/questions/2168475/django-admin-how-to-sort-by-one-of-the-custom-list-display-fields-that-has-no-d – user2390182 Nov 28 '15 at 13:00
  • Thank you for the link, I had already read it and tried before asking... unfortunately, and you can't precise to use the custom "non-field" itself like mot_avec_groupe.admin_order_field = 'mot_avec_groupe' because you'll get this error: "Cannot resolve keyword u'mot_avec_groupe' into field. Choices are: groupes, id, texte" – Olivier Pons Nov 28 '15 at 16:07

1 Answers1

0

You should add this method as a property.

class Mot(models.Model):
    groupes = models.ManyToManyField(Groupe)
    texte = models.CharField(max_length=50)

    def _mot_avec_groupe(self):
        return str(self)
    mot_avec_groupe = property(_mot_avec_groupe)
    mot_avec_groupe.short_description = _(u'Description')
    mot_avec_groupe.allow_tags = True
maciek
  • 3,198
  • 2
  • 26
  • 33
  • I tried your solution but it doesn't work. The documentation in your link in your answer explains that **Django does all the sorting at the database level**. So I think I won't be able to solve this. – Olivier Pons Nov 28 '15 at 18:03