L
L
leoZZ2021-11-15 17:11:27
Django
leoZZ, 2021-11-15 17:11:27

Display model fields in django admin via list_display?

class ExampleModel(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    all_free_time = 0


class ExampleAdmin(admin.ModelAdmin):
    model = ExampleModel
    list_display = ("first_name", "last_name",  "get_free_time")

    @admin.display(ordering='all_free_time', description='Free hours')
    def get_free_time(self, obj):
        return obj.all_free_time
admin.site.register(ExampleModel, ExampleAdmin)


There is some logic that fills all_free_time and saves via obj.save(). Within this code interval, everything is OK, but the admin panel refers to the default null value in the model and this is logical in principle, but is it possible to get around this?
Is it possible to somehow display the all_free_time field in the django admin via list_display? I understand that you can do this all_free_time = models.IntegerField(default=0), but still.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question