﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
12521	ModelAdmin save_model request.user idiom is broken by model validation	simon	jkocherhans	"The common (documented) pattern for automatically setting the ""author"" field on an object  to request.user by over-riding the save_model method on a ModelAdmin subclass breaks with the new model validation code. Here are the example models.py and admin.py files:

{{{
from django.db import models

class Entry(models.Model):
    title = models.CharField(max_length = 255)
    body = models.TextField()
    published = models.DateTimeField(auto_now_add = True)
    author = models.ForeignKey('auth.User')
    
    def __unicode__(self):
        return self.title
}}}
admin.py:
{{{
from models import Entry
from django.contrib import admin

class EntryAdmin(admin.ModelAdmin):
    list_display = ('title', 'published', 'author')
    exclude = ('author',)
    
    def save_model(self, request, obj, form, change):
        obj.author = request.user
        obj.save()

admin.site.register(Entry, EntryAdmin)
}}}
And here's the exception:
{{{
Traceback:
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/core/handlers/base.py"" in get_response
  99.                     response = callback(request, *callback_args, **callback_kwargs)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/contrib/admin/options.py"" in wrapper
  237.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/utils/decorators.py"" in __call__
  36.         return self.decorator(self.func)(*args, **kwargs)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/utils/decorators.py"" in _wrapped_view
  86.                     response = view_func(request, *args, **kwargs)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/utils/decorators.py"" in __call__
  36.         return self.decorator(self.func)(*args, **kwargs)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/views/decorators/cache.py"" in _wrapped_view_func
  70.         response = view_func(request, *args, **kwargs)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/contrib/admin/sites.py"" in inner
  187.             return view(request, *args, **kwargs)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/utils/decorators.py"" in _wrapped_view
  86.                     response = view_func(request, *args, **kwargs)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/db/transaction.py"" in _commit_on_success
  295.                     res = func(*args, **kw)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/contrib/admin/options.py"" in add_view
  760.             if form.is_valid():
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/forms/forms.py"" in is_valid
  120.         return self.is_bound and not bool(self.errors)
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/forms/forms.py"" in _get_errors
  111.             self.full_clean()
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/forms/forms.py"" in full_clean
  286.             self.cleaned_data = self.clean()
File ""/Users/simon/Development/playing-with-django-1.2/ve/lib/python2.6/site-packages/django/forms/models.py"" in clean
  270.                 raise UnresolvableValidationError(e.message_dict)

Exception Type: UnresolvableValidationError at /admin/blog/entry/add/
Exception Value: 
}}}
The validation error is ""This field cannot be null"".

This pattern is documented here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-methods - we should at the very least update the documentation, but even having done that we'd still break existing code."		closed	contrib.admin	dev		fixed		em@… Jannis Leidel knutin@… zbyte64@… Maniac@… aribao@…	Design decision needed	1	0	1	0	0	0
