How to create an account approval admin page in Django
I'm trying to create an admin page in Django to approve user accounts. Here are the specific requirements:
Background
- I have a landlord registration system. Each landlord must provide personal information and details about their rental property when registering.
- Landlord accounts are inactive by default. Administrators need to review the property information before activating the account.
Main Models
# User model
class User(AbstractUser):
user_type = models.CharField(max_length=10, choices=UserType, default=UserType.TENANT)
phone_number = models.CharField(max_length=10, unique=True, blank=True, null=True)
# Other fields...
# Property model
class Property(models.Model):
owner = models.ForeignKey("accounts.User", on_delete=models.CASCADE, related_name="properties")
name = models.CharField(max_length=256)
status = models.CharField(max_length=10, choices=PropertyStatus.choices, default=PropertyStatus.PENDING)
# Other fields...
Question
I want to create a Django admin page that:
- Displays a list of unapproved landlord accounts
- When clicking on an account, shows detailed user information and their registered property details
- Has functionality to approve or reject the account (if the property is approved, the account will be activated)
I've thought about using a custom ModelAdmin
with readonly_fields
to display detailed information, but I'm not clear on the best way to:
- Display information from multiple models (User and Property) in the same admin page
- Add actions to approve/reject accounts
What's the best way to implement this? Code examples would be very helpful. Thank you!
What I've tried
class PendingLandlordAdmin(admin.ModelAdmin):
list_display = ['username', 'email', 'property_name']
readonly_fields = ['username', 'email', 'property_details']
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.filter(user_type='LANDLORD', is_active=False)
# Not sure how to implement property_details and actions...
admin.site.register(User, UserAdmin)
admin.site.register(User, PendingLandlordAdmin) # Doesn't work
The approach I tried doesn't work because I can't register 2 admin pages for 1 model