1

My Django project is based on built-in User model.

For some extra attributes, I have defined another model:

models.py:

class Status(models.Model):
    email = models.ForeignKey(User, on_delete=models.CASCADE)

    is_verified = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

    def __str__(self):
        return self.email

And here's the custom ModelAdmin:

admin.py:

class CustomUserAdmin(UserAdmin):
    model = User
    list_display = ['email']
    search_fields = ['email']

I want list_display to show me is_verified and is_active fields from Status model but I'm lost.

I've looked into some similar questions on SO like Display field from another model in django admin or Add field from another model Django but none of the solutions are applicable because one of my models is Django's built-in.

Omid Shojaee
  • 333
  • 1
  • 4
  • 20

1 Answers1

2

Try this.

models.py

class Status(models.Model):
    # email = models.ForeignKey(User, on_delete=models.CASCADE)
    email = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        related_name='status'
    )

    is_verified = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

    def __str__(self):
        return self.email

admin.py

class CustomUserAdmin(UserAdmin):
    model = User
    list_display = ['email', 'is_verified', 'is_active']
    search_fields = ['email']
    
    def is_verified(self, obj):
        return obj.status.is_verified
    
    def is_active(self, obj):
        return obj.status.is_active

If you want to apply the displayed name or ordering, please refer to the following source


I saw your comments and tried running the code myself. There were some problems, so I modified admin.py as follows.

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

admin.site.unregister(User)


@admin.register(User)
class CustomUserAdmin(UserAdmin):
    list_display = ['email', 'status_is_verified', 'status_is_active']
    list_filter = ['status__is_verified', 'status__is_active']
    search_fields = ['email']
    
    @admin.display(ordering='status__is_verified')
    def status_is_verified(self, obj):
        return obj.status.is_verified
    
    @admin.display(ordering='status__is_active')
    def status_is_active(self, obj):
        return obj.status.is_active
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
gypark
  • 251
  • 1
  • 8
  • One question: why I can use ```is_verified``` in ```list_display``` but not in ```list_filter```? – Omid Shojaee Sep 20 '22 at 08:42
  • And why in admin site ```is_verified``` shows ```True/False``` instead of that green/white tick displayed for ```STAFF STATUS``` and ```SUPERUSER STATUS```? – Omid Shojaee Sep 20 '22 at 08:51
  • I know the difference in how to use `list_display` and `list_filter`, but I don't know why it was made that way. – gypark Sep 20 '22 at 12:58
  • 1
    and Please read the following article for the bool type to be displayed as a geen icon. https://stackoverflow.com/questions/8227023/list-display-boolean-icons-for-methods – gypark Sep 20 '22 at 13:04