The good thing about Django is that the admin page is powerful, but if you use it properly, ORM will issue miscellaneous SQL, so take care. I'm just starting to use it, so I'll add it as needed.
Select_related is enabled if list_display contains a ForeignKey field. However, select_related does not follow the relation of FK field with null = True, so the target is fetched by individual SQL for the number of records. Therefore, when the list page is opened, the SELECT statement is issued about 400 times. Also, select_related traces the FK field with null = False indefinitely, so it may be SQL that JOINs with 10 or 20 tables.
To do it well, define queryset yourself. With this, you can join as much as necessary.
admin.py
class UserRelationshipAdmin(admin.ModelAdmin):
list_display = ('id', 'from_user', 'to_user', 'is_block', 'is_friend', 'created_at')
def queryset(self, request):
return super(UserRelationshipAdmin,
self).queryset(request).select_related('from_user', 'to_user')
Foreign Key and choices fields are displayed by pull-down (select element) on the detail page by default. It will be great if tens of thousands of tables are referenced, so if you specify raw_id_fields, it will display a text box + label.
admin.py
class UserProfileAdmin(admin.ModelAdmin):
raw_id_fields = ('user',)
There is a description here in the official document https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields
Recommended Posts