Version 7 (modified by oyvind, 9 years ago) (diff)


How to set the current user on the model instance using newforms admin.

TODO make it work with inline edited models

# app/

from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    user = models.ForeignKey(User)
    content = models.TextField()

# app/

from app.models import Post
from django.contrib import admin

class PostModelAdmin(admin.ModelAdmin):

    fields= ('content',)

    def __call__(self, request, url):

        self.user = request.user

        return super(PostModelAdmin, self).__call__(request, url)

    def save_model(self, request, form, change): 

        new_object =

        new_object.user = self.user

        return new_object, PostModelAdmin)

Back to Top