﻿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
18181	Not filling out a blank=True ForeignKey admin raw_id field raises ValueError because it tries to set the foreign key to None	James Aylett	James Aylett	"For a model with:

{{{
    created_by = models.ForeignKey(User, related_name='created_%(class)s_set', null=False, blank=True)
}}}

(via an abstract base model, hence the pattern in `related_name`). Submitting an admin with `created_by` in `raw_id_fields`, but not filling out the field (ie leaving it blank) results in the following backtrace:

{{{
File ""/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py"", line 111, in get_response
  response = callback(request, *callback_args, **callback_kwargs)
File ""/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py"", line 307, in wrapper
  return self.admin_site.admin_view(view)(*args, **kwargs)
File ""/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"", line 93, in _wrapped_view
  response = view_func(request, *args, **kwargs)
File ""/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py"", line 79, in _wrapped_view_func
  response = view_func(request, *args, **kwargs)
File ""/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py"", line 197, in inner
  return view(request, *args, **kwargs)
File ""/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"", line 28, in _wrapper
  return bound_func(*args, **kwargs)
File ""/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"", line 93, in _wrapped_view
  response = view_func(request, *args, **kwargs)
File ""/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py"", line 24, in bound_func
  return func(self, *args2, **kwargs2)
File ""/usr/local/lib/python2.7/dist-packages/django/db/transaction.py"", line 217, in inner
  res = func(*args, **kwargs)
File ""/usr/local/lib/python2.7/dist-packages/django/contrib/admin/options.py"", line 864, in add_view
  if form.is_valid():
File ""/usr/local/lib/python2.7/dist-packages/django/forms/forms.py"", line 121, in is_valid
  return self.is_bound and not bool(self.errors)
File ""/usr/local/lib/python2.7/dist-packages/django/forms/forms.py"", line 112, in _get_errors
  self.full_clean()
File ""/usr/local/lib/python2.7/dist-packages/django/forms/forms.py"", line 269, in full_clean
  self._post_clean()
File ""/usr/local/lib/python2.7/dist-packages/django/forms/models.py"", line 308, in _post_clean
  self.instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
File ""/usr/local/lib/python2.7/dist-packages/django/forms/models.py"", line 50, in construct_instance
  f.save_form_data(instance, cleaned_data[f.name])
File ""/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py"", line 416, in save_form_data
  setattr(instance, self.name, data)
File ""/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py"", line 327, in __set__
  (instance._meta.object_name, self.field.name))
ValueError: Cannot assign None: “Collection.created_by” does not allow null values.
}}}

It should instead display a validation failure for that field in the admin page.

Forcibly setting `required=True` on the field as created in `django.contrib.admin.options:BaseModelAdmin.formfield_for_foreignkey()` (or in the `ModelAdmin` subclass in the app's `admin.py`) results in the desired behaviour, but probably isn't actually the correct approach.

This is similar to #9484, which was deduped into #8746, the final patch for which didn't deal with this, although it did deal with `ValueError` in another situation. See also [https://code.djangoproject.com/ticket/8746#comment:12 a late comment on 8746] which is the same as the issue here, and should have become a new ticket (and may have done, but I can't find it).

It is possible that this has been fixed in 1.4, which I can't migrate to yet; if so, then at least this ticket will capture that for anyone else who has the problem while 1.3 is still common."	Uncategorized	closed	contrib.admin	1.3	Normal	invalid			Unreviewed	0	0	0	0	0	0
