Version 8 (modified by oyvind, 10 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 save_model(self, request, form, change): 

        new_object =

        new_object.user = request.user

        return new_object, PostModelAdmin)

Back to Top