Auto-populating created_by field with Django admin site


I want to use the Django admin interface for a very simple web application but I can't get around a problem that should not be that hard to resolve ..

Consider the following:

class Contact(models.Model):
    name = models.CharField(max_length=250, blank=False)
    created_by = models.ForeignKey(User, blank=False)

I can't find a way to auto-populate the created_by field and make the Django admin aware of it. Most of the method I've seen implies overloading the Object's save method and pass it the request user. They all requires to build your custom views and/or forms.

Optimally the form to create new contacts in the admin site should not show the created_by field (which is quite easy) and auto-populate it with the current user (which seems harder than it should).

5/13/2009 2:42:01 AM

Accepted Answer

Involves implementing save methods on your ModelAdmin objects.

5/13/2009 2:50:42 AM

You need to specify a default for the field, in this case a method call that gets the current user (see the auth documentation to get the current user).

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow