[Answer]-Display owner of a post in django admin list display

1👍

Try to use User model.

models.py

from django.contrib.auth.models import User

    class Activity(models.Model):
        sub_choice = ((1, 'English'), (2, 'Math'),
                      (3, 'Physics'), (4, 'Chemistry'))
        subject = models.IntegerField(choices=sub_choice, default=1, max_length=50)
        hours = models.IntegerField(verbose_name='Time spent in hours', default=0)
        owner = models.ForeignKeyField(User)

admin.py

    class ActivityAdmin(ModelAdmin):
        list_display = ('subject', 'hours', 'owner')
        exclude = ['owner']

        def save_model(self, request, obj, form, change):
            """
            Given a model instance save it to the database.
            """
            obj.owner = request.user
            obj.save()

admin.site.register(Activity, ActivityAdmin)

Leave a comment