I would like to use a queryset(queryset) in django-admin to get all the objects. Based on the queryset achieved, i would like to display certain attributes of the queryset for viewing. For example, I would like to show the person’s id, the person’s parent, the person’s parentname,email and so on.
The example provided in Django docs is:
class MyModelAdmin(admin.ModelAdmin):
def queryset(self, request):
qs = super(MyModelAdmin, self).queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(author=request.user)
I would definitely like to improve on this.
I have tried something like this:
class ChildParentAdmin(admin.ModelAdmin):
list_display = ('displayname', 'StudentID',)
search_fields = ['displayname',]
def queryset(self, request):
qs = super(MyModelAdmin, self).queryset(request)
def StudentID(self,queryset):
for student in queryset:
return student.pk
The point i am stuck in how do you use the queryset and perform further actions like getting the id of each child, name of parent of each child and so on… Would like some guidance on going about doing this.Would really appreciate any help on this.. Thanks…
This is the correct way of doing it.. You don’t have to query over all the objects in queryset. Just need to pass the obj. It will run it for all objects. There are other ways of doing this, which doesn’t involve the class itself..To read more on this, please look at list_display Django Admin. The examples are very clear.