Alexa Iulian
Alexa Iulian

Reputation: 520

How to filter objects in admin?

How i can filter objects that are shown in wagtail admin page of each model? In django this problem can be solved by overriding Admin class of model, in wagtail i dont know how it is possibile.

Upvotes: 0

Views: 349

Answers (1)

Loïc Teixeira
Loïc Teixeira

Reputation: 1434

Assuming you're talking about non-page objects, you have two options to expose them in the admin interface: snippets and ModelAdmin.

The later is very similar to Django's ModelAdmin (but is not the same) and you should be able to overwrite get_queryset to filter the objects like you are used to with Django.

For example, after setting up the ModelAdmin app correctly, you can do something like this:

# models.py
class Person(django.db.models.Model):
  type = django.db.models.CharField(max_length=20, choices=(('student', 'Student'), ('teacher', 'Teacher')))
  # ...

# wagtail_hooks.py
class StudentAdmin(wagtail.contrib.modeladmin.options.ModelAdmin):
  model = my_app.models.Person

  def get_queryset(self, request):
    qs = super(StudentAdmin, self).get_queryset(request)
    return qs.filter(type='student')

wagtail.contrib.modeladmin.options.modeladmin_register(MyPageModelAdmin)

Upvotes: 1

Related Questions