Opened 8 years ago

Closed 8 years ago

#10075 closed (fixed)

model inheritance with foreign key problem

Reported by: marcominutoli@… Owned by: nobody
Component: Forms Version: 1.0
Severity: Keywords: model inheritance foreign key
Cc: raffaele.salmaso@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Ramiro Morales)

I have these three models:

class Address(models.Model):
   name = models.CharField(max_length=100)
   ## many other fields ##

   class Meta:
       ordering = ('name',)

   def __unicode__(self):

class Organization(models.Model):
   name = models.CharField(max_length=100)
   ## many other fields ##

   class Meta:
       ordering = ('name',)

   def __unicode__(self):

class OrganizationAddress(Address):
   organization = models.ForeignKey (
       related_name = "addresses",

When i try to add/edit an organization and add one address i've got this error:

      " ValueError: Cannot assign None: "OrganizationAddress.address_ptr" does not allow null values. "

I've tried to understand the problem (because it happened since one day to another..) and i've found that the problem was born after the django commit of the changeset [9664]

This is the "incriminated code" of changeset:

	488	        if fk_attname == self._pk_field.attname: 
 	489	            exclude =  [] 
 	490	        else: 
 	491	            exclude = []

I have "organization_id" in the fk_attname e "address_ptr" in the self._pk_field.attname and so the primary_key wasn't insert in the exclude list.

Below a code portion to test the problem.

from django.forms.models import inlineformset_factory
from my_apps.models import Organization, Address, OrganizationAddress

org = Organization.objects.get(name='test')

inline_formset = inlineformset_factory(Organization, OrganizationAddress, can_delete=False, extra=2)

data = {
   'addresses-TOTAL_FORMS': '2', # the number of forms rendered
   'addresses-INITIAL_FORMS': '0', # the number of forms with initial data
   'addresses-0-name': 'Test test',

formset = inline_formset(data, instance=org)


Sorry for my english.
Marco M.

Attachments (1)

10075.diff (2.7 KB) - added by Karen Tracey 8 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 8 years ago by Ramiro Morales

Description: modified (diff)
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

(edited description, please please use the 'Preview' button when submitting a ticket)

comment:2 Changed 8 years ago by anonymous

Cc: raffaele.salmaso@… added

Changed 8 years ago by Karen Tracey

Attachment: 10075.diff added

comment:3 Changed 8 years ago by Karen Tracey

Triage Stage: UnreviewedAccepted

OK, I attached a patch that slightly modifies the fix from r9664 to handle this case. I'm kind of guessing, though, since I'm not very familiar with the inheritance implementation nor the intricate details of inline formsets. Review from someone with more of a clue in this area would be appreciated.

comment:4 Changed 8 years ago by anonymous

Tested with incriminated code and everything work as expected (=as before r9664).

comment:5 Changed 8 years ago by Simon Litchfield

See somewhat related #10180.

comment:6 Changed 8 years ago by Malcolm Tredinnick

Karen, this looks like a fix, at least, and we should probably commit it to make things more or less work. It feels a bit messy, having to special-case things like that and it might be a variation on the problem I want to fix that #9994 is the tip of the iceberg for. No amazing (internal) API comes to mind, though.

comment:7 Changed 8 years ago by Karen Tracey

Resolution: fixed
Status: newclosed

(In [9811]) Fixed #10075: Allowed saving of inline-edited models that use multi-table inheritance.

r9809 from trunk.

Note: See TracTickets for help on using tickets.
Back to Top