Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#9498 closed (fixed)

generic inline formsets: object has no attribute 'fk'

Reported by: Fred Bartle Owned by: Brian Rosner
Component: Contrib apps Version: master
Severity: Keywords: generic inline formsets
Cc: Carl Meyer, Ben Spaulding Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


As of r9297, which django.contrib.admin.helpers now utilizes, generic inline formsets now break attempting to access Attached is a simple fix, though I suspect there's more work to be done with generic and r9297.

Attachments (4) (504 bytes) - added by Fred Bartle 10 years ago. (728 bytes) - added by markus _forgot_ evigo _spam_ net 10 years ago.
generic.py_helpers.py_r9329.diff (892 bytes) - added by markus _uploading_ evigo _patch_ net 10 years ago.
generic_inline_tests.diff (6.1 KB) - added by Daniel Roseman 10 years ago.

Download all attachments as: .zip

Change History (14)

Changed 10 years ago by Fred Bartle

Attachment: added

comment:1 Changed 10 years ago by Fred Bartle

Has patch: set
Needs tests: set

comment:2 Changed 10 years ago by Fred Bartle

Here's a basic example:

class Attribute(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    key = models.CharField(max_length=30)
    value = models.CharField(max_length=30)

class MyModel(models.Model):
    name = models.CharField(max_length=30)
    attributes = generic.GenericRelation(Attribute)

class AttributeInline(generic.GenericTabularInline):
    model = Attribute

class MyModelAdmin(models.Model):
    inlines = [AttributeInline]

comment:3 Changed 10 years ago by Brian Rosner

Owner: changed from nobody to Brian Rosner
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:4 Changed 10 years ago by markus@…

applying the patch helped, but directly showed a new bug:

looking into contrib/contenttypes/ I found this:

if exclude is not None: 
    exclude = [,]

makes no sense to me, but i'm not the architect ;)
So i'm attaching another patch, which includes the first and
deletes the whole "else" - for me this fixes GenericTabularInline

Changed 10 years ago by markus _forgot_ evigo _spam_ net

Attachment: added

comment:5 Changed 10 years ago by markus _presents_a_better_ evigo _solution_ net

so once again:

after some testing i've seen, that those lines:

if exclude is not None:
    exclude = [,]

Are indeed _not_ useless! It's absolutly clear (now - for me - finally), that
we of course don't need the ForeignKey in a GenericInlineFormSet, because it's
clear that we set the ForeignKey according the Model we are "inlining". So without
the "else" from the snippet, I always had a content_type and object_id field in
my inline-form, what seems to be a new-self-produced-bug - means: re-add those two
lines and restart searching, why I always get:

The problem with this is, the fk_field is beeing called directly from the template,
so there is no way to avoid beeing asked for the fk_field-widget. As mentioned before,
we have absolutly no need for the fk-field, as it gets set automaticly by the parent
model. So i just looked for the output-method for fk_field - in contrib/admin/

def fk_field(self):
        return AdminField(self.form,, False)

inside "InlineAdminForm", means for me only inlined-models come across this form,
so it's painless to just change it to:

def fk_field(self):
        return AdminField(self.form,, False)
    except KeyError, e:
        return ""

This actually is just a silent failure, which still works if needed.
(Isn't this anyway planned/recommended for template-parsing?)

So there is another file attached, including this and the first patch.
Do _NOT_ use the second patch, it's more an ugly hack than a solution ;)

hope i could help,

Changed 10 years ago by markus _uploading_ evigo _patch_ net

comment:6 Changed 10 years ago by Carl Meyer

Cc: Carl Meyer added

comment:7 Changed 10 years ago by Ben Spaulding

Cc: Ben Spaulding added

Changed 10 years ago by Daniel Roseman

Attachment: generic_inline_tests.diff added

comment:8 Changed 10 years ago by Daniel Roseman

Needs tests: unset

Patch that adds regression tests for this problem.

comment:9 Changed 10 years ago by Brian Rosner

Resolution: fixed
Status: assignedclosed

(In [9413]) [1.0.X] Fixed #9498 -- Handle a formset correctly when the foreign key is not available (for now).

This case pops up with generic foreign key inlines after [9297]. Added tests
to handle future regressions with generic foreign key inlines in the admin.

Thanks markus and danielr for patches.

Backport of [9412] from trunk.

comment:10 Changed 10 years ago by Brian Rosner

This was also applied on trunk in [9412] -- dang post-commit hook :)

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