What I aim to achieve in the end is to combine the list of all users (from auth.models) with a DateTimeField from another model (model.Model). What's the best way of doing this?
My code:
survey/models/response.py
from django.contrib.auth.models import User
from django.db import models
class Response(models.Model):
created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, null=True, blank=True)
In admin.py, I can create a custom user class which lists all of the registered users' usernames like so:
survey/admin.py
from django.contrib.auth.models import User
admin.site.unregister(User)
class UserAdmin(admin.ModelAdmin):
list_display = ('username',)
admin.site.register(User, UserAdmin)
Essentially I want extend UserAdmin as list_display = ('username', 'created') - with username coming from auth.models and created coming from models.Model.
At the moment I can create a class in admin.py that shows me a list of all the users who have submitted a Response. The issue is that if you have 200 users, it is more useful to see who hasn't submitted from a list of all users than only listing the users who have submitted a Response.
You can write a function to fetch relevant data for you to show in the admin listing page.
In your admin.py
from django.contrib.auth.models import User
admin.site.unregister(User)
class UserAdmin(admin.ModelAdmin):
list_display = ('username', 'latest_response')
def latest_response(self, obj):
return obj.response_set.latest('created') # Or any other logic you want to apply here to return a string or boolean that can be shown on the admin
admin.site.register(User, UserAdmin)