Answer the question
In order to leave comments, you need to log in
How to display model fields in admin panel?
I have a "UserLevel" model. You need to display all the fields from this model in the admin panel.
class UserLevel(models.Model):
level = models.ForeignKey(Level, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
progress = models.IntegerField(default=0)
def __str__(self):
return str(self.user)
Answer the question
In order to leave comments, you need to log in
Solution:
model.py
class UserLevel(models.Model):
level = models.ForeignKey(Level, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
progress = models.IntegerField(default=0)
def __str__(self):
return str(self.level)
@admin.register(UserLevel)
class UserLevelAdmin(admin.ModelAdmin):
list_display = ('level_id', 'user', 'level', 'progress')
list_select_related = (
'level',
)
Write in the admin class for UserLevel list_display
Example:
class UserLevelAdmin(admin.ModelAdmin):
list_display = ('id', 'level', 'user', 'progress')
admin.site.register(UserLevel, UserLevelAdmin)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question