M
M
Mike2018-10-27 21:26:54
Django
Mike, 2018-10-27 21:26:54

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)

This works, but I don't know how to add other fields.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mike, 2018-10-28
@google_online

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.py
@admin.register(UserLevel)
class UserLevelAdmin(admin.ModelAdmin):

    list_display = ('level_id', 'user', 'level', 'progress')
    list_select_related = (
        'level',
    )

R
Rostislav Grigoriev, 2018-10-27
@crazyzubr

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)

K
Konstantin Malyarov, 2018-10-28
@Konstantin18ko

list_display = ('__all__')
Not sure, but you can.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question