Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter by a virtual name in the admin site

Django4.1

class Exercise(NameMixin,
               UrlMixin,
               PriorityMixin,
               CommentMixin):
    unit = models.ForeignKey('vocabulary_units.Unit',
                             on_delete=models.CASCADE,
                             null=True, )



class Phrase(models.Model):

    exercise = models.ForeignKey('vocabulary_exercises.Exercise',
                                 on_delete=models.CASCADE,
                                 null=True,
                                 blank=False)

    @property
    def unit(self):
        result = self.exercise.unit
        return result

A phase belongs to an exercise, excercise belongs to a unit.

The problem is that in the admin site I’d like to organise for phrases a filter by unit.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is it possible?

>Solution :

You can use ‘__’ lookup method in the list_filter fields. Also you didn’t post how the Unit model looks, this code will filter in IDs basically, you probably will want to use exercise__unit__name or similar:

from django.contrib import admin


class PhraseAdmin(admin.ModelAdmin):
    list_filter = ['exercise__unit']

Check also this answer

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading