Display owner of a post in django admin list display -
my model :
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)
my admin.py looks :
class activityadmin(modeladmin): list_display = ('subject', 'hours') admin.site.register(activity, activityadmin)
in list display,i want first name of user created activty.how do without explicitly asking creator of activity put name in form.is there way information using custom function user model ?
try 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 model instance save database. """ obj.owner = request.user obj.save() admin.site.register(activity, activityadmin)
Comments
Post a Comment