views:

1149

answers:

2

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  A: 

http://code.djangoproject.com/wiki/CookBookNewformsAdminAndUser

Involves implementing save methods on your ModelAdmin objects.

jsamsa
That's exactly what I was searching, thanks !
h3
A: 

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).

Tom Leys