Vollmilchbb
Vollmilchbb

Reputation: 481

Django Allauth filter "Email addresses" in Admin


I'm using django-allauth and I have a question. In order to filter the data in my adminsite for my User model I use following code:

def get_queryset(self, request):
    qs = super(UserAdmin, self).get_queryset(request)
    if request.user.is_superuser:
        return qs
    return qs.filter(profile__country=request.user.profile.country)

It lets the adminusers (not superusers) only see the tables from users in their country.

I would also like to filter the data in the standard column created by allauth called "Accounts" - > "Email Addresses" but I don't know which queryset that would be. I would be thankful for any tip.

Have a nice day guys!

Solution here: ->

admin.py looks like this :

from allauth.account.models import EmailAddress
from allauth.account.admin import EmailAddressAdmin


class CustomAllauthAdmin(EmailAddressAdmin):

    def get_queryset(self, request):
        qs = super(EmailAddressAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(user__profile__country=request.user.profile.country)

admin.site.unregister(EmailAddress)
admin.site.register(EmailAddress, CustomAllauthAdmin) 

Upvotes: 0

Views: 1704

Answers (1)

onyeka
onyeka

Reputation: 1537

Since it's a third party app, you'll first need to override its admin in your own admin.py. Check the answer here for an example.

As for the queryset, you inspect the allauth model for the EmailAddress model. It links to users using a 'user' field. So, you would chain your query like so:

 qs = super(EmailAddressAdmin, self).get_queryset(request)
    if request.user.is_superuser:
        return qs
    return qs.filter(user__profile__country=request.user.profile.country)

Upvotes: 3

Related Questions