﻿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
2751	ManyToManyField in models with edit_inline breaks the admin	bangcok_dangerus@…	Adrian Holovaty	"A model that contains a ForeignKey with the edit_inline option breaks the admin if it also contains a ManyToManyField. The two problems I've found are:

1) Contrary to the docs, 'core=True' doesn't seem to do apply to a ManyToManyField. Example:

{{{
class Car(models.Model):
	driver = models.ForeignKey(Driver, edit_inline=models.STACKED)
	parts = models.ManyToManyField(Part, core=True)
}}}

The above code produces the error

{{{
test.car: At least one field in Car should have core=True, because it's being edited inline by driver.Driver.
}}}


2) Once the above problem is fixed using something like the code below, the admin interface will successfully display the inline Car objects, including the ManyToManyField. As long the Car objects are created and edited using their own admin pages (i.e. NOT inline), things seem to work fine.

{{{
class Car(models.Model):
	name = models.CharField(maxlength=20, core=True)
	driver = models.ForeignKey(Driver, edit_inline=models.STACKED)
	parts = models.ManyToManyField(Part, core=True)
}}}

However, attempting to create or edit a car object inline with a Driver creates the following traceback:

{{{
Traceback (most recent call last):
File ""/usr/local/lib/python2.4/site-packages/django/core/handlers/base.py"" in get_response
  74. response = callback(request, *callback_args, **callback_kwargs)
File ""/usr/local/lib/python2.4/site-packages/django/contrib/admin/views/decorators.py"" in _checklogin
  55. return view_func(request, *args, **kwargs)
File ""/usr/local/lib/python2.4/site-packages/django/views/decorators/cache.py"" in _wrapped_view_func
  39. response = view_func(request, *args, **kwargs)
File ""/usr/local/lib/python2.4/site-packages/django/contrib/admin/views/main.py"" in change_stage
  329. new_object = manipulator.save(new_data)
File ""/usr/local/lib/python2.4/site-packages/django/db/models/manipulators.py"" in save
  218. was_changed = getattr(new_rel_obj, 'set_%s' % f.name)(rel_new_data[f.attname])

  AttributeError at /admin/test/driver/1/
  'Car' object has no attribute 'set_parts'
}}}

Additionally, trying to add a Driver (with the inline Cars left untouched) creates the traceback:

{{{
Traceback (most recent call last):
File ""/usr/local/lib/python2.4/site-packages/django/core/handlers/base.py"" in get_response
  74. response = callback(request, *callback_args, **callback_kwargs)
File ""/usr/local/lib/python2.4/site-packages/django/contrib/admin/views/decorators.py"" in _checklogin
  55. return view_func(request, *args, **kwargs)
File ""/usr/local/lib/python2.4/site-packages/django/views/decorators/cache.py"" in _wrapped_view_func
  39. response = view_func(request, *args, **kwargs)
File ""/usr/local/lib/python2.4/site-packages/django/contrib/admin/views/main.py"" in add_stage
  254. new_object = manipulator.save(new_data)
File ""/usr/local/lib/python2.4/site-packages/django/db/models/manipulators.py"" in save
  198. new_rel_obj.save()
File ""/usr/local/lib/python2.4/site-packages/django/db/models/base.py"" in save
  204. ','.join(placeholders)), db_values)
File ""/usr/local/lib/python2.4/site-packages/django/db/backends/util.py"" in execute
  12. return self.cursor.execute(sql, params)

  IntegrityError at /admin/test/driver/add/
  ERROR: null value in column ""name"" violates not-null constraint INSERT INTO ""test_car"" (""driver_id"") VALUES (10)
}}}

The model used for the driver is simply:

{{{
class Driver(models.Model):
	name = models.CharField(maxlength=20)
}}}

Even more strangely, returning to the admin interface after the exception reveals that the driver WAS actually created.

-Basil.
"	defect	closed	contrib.admin	dev	normal	duplicate			Unreviewed	0	0	0	0	0	0
