[Django] #32597: generic inline formsets: object has no attribute 'fk'

10 views
Skip to first unread message

Django

unread,
Mar 26, 2021, 6:36:51 PM3/26/21
to django-...@googlegroups.com
#32597: generic inline formsets: object has no attribute 'fk'
-------------------------------------+-------------------------------------
Reporter: etanter | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.2
Severity: Normal | Keywords: generic field,
Triage Stage: | inline formset
Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi,

I think I'm facing a zombie-bug reminiscent of ticket #9498:
https://code.djangoproject.com/ticket/9498
From 12 years ago and marked as solved.


The setting is simple: I have a model with a generic field (with non-
standard names). The corresponding inline class declares the `ct_field`
and `ct_fk_field` as required.

If I don't try to customize the formset, then all works fine. However, as
soon as I declare a formset attribute for the inline (even if I use
`formset=BaseInlineFormSet`), it crashes with an error:

`type object 'DocumentFormSet' has no attribute 'fk'`

With the debugger I traced the problem down to the execution of this
method in BaseInlineFormSet (during the initialization process of the
formsets):

{{{#!python
@classmethod
def get_default_prefix(cls):
return
cls.fk.remote_field.get_accessor_name(model=cls.model).replace('+', '')
}}}

Problem is: `cls` has no field `fk` (it has `ct_fk_field` and `ct_field`
properly set, though).

In fact, the instance of `BaseInlineFormSet` has a field `fk`, used in
many instance methods, but the class itself does not :-(

I've done the following "fix":

1. redefine the get_default_prefix method to use `ct_field` instead of
`fk`

{{{#!python
@classmethod
def get_default_prefix(cls):
return
cls.ct_field.remote_field.get_accessor_name(model=cls.model).replace('+',
'')
}}}


2. at the instance methods level, I then had the same errors (`no
attribute 'fk'`), so I defined:

{{{#!python
@property
def fk(self):
return self.ct_field
}}}
And it works (I tried defining an `empty_form` property to set an initial
value of my inlines, and it behaves as expected.

Is that a known bug of 2.2? is it fixed upstream? Or am I missing
something and there's some documentation somewhere explaining what to do
in that case? (my fix is clearly not intended for users to do, I believe).

Thanks

Éric

--
Ticket URL: <https://code.djangoproject.com/ticket/32597>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 26, 2021, 6:37:22 PM3/26/21
to django-...@googlegroups.com
#32597: generic inline formsets: object has no attribute 'fk'
-------------------------------------+-------------------------------------
Reporter: etanter | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.2
Severity: Normal | Resolution:

Keywords: generic field, | Triage Stage:
inline formset | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by etanter:

Old description:

New description:

Hi,

I think I'm facing a zombie-bug reminiscent of ticket #9498:
https://code.djangoproject.com/ticket/9498
From 12 years ago and marked as solved.


The setting is simple: I have a model with a generic field (with non-
standard names). The corresponding inline class declares the `ct_field`
and `ct_fk_field` as required.

If I don't try to customize the formset, then all works fine. However, as
soon as I declare a formset attribute for the inline (even if I use
`formset=BaseInlineFormSet`), it crashes with an error:

`type object 'DocumentFormSet' has no attribute 'fk'`

With the debugger I traced the problem down to the execution of this
method in BaseInlineFormSet (during the initialization process of the
formsets):

{{{#!python
@classmethod
def get_default_prefix(cls):
return
cls.fk.remote_field.get_accessor_name(model=cls.model).replace('+', '')
}}}

Problem is: `cls` has no field `fk` (it has `ct_fk_field` and `ct_field`
properly set, though).

I've done the following "fix":

1. redefine the get_default_prefix method to use `ct_field` instead of
`fk`

{{{#!python
@classmethod
def get_default_prefix(cls):
return
cls.ct_field.remote_field.get_accessor_name(model=cls.model).replace('+',
'')
}}}


2. at the instance methods level, I then had the same errors (`no
attribute 'fk'`), so I defined:

{{{#!python
@property
def fk(self):
return self.ct_field
}}}
And it works (I tried defining an `empty_form` property to set an initial
value of my inlines, and it behaves as expected.

Is that a known bug of 2.2? is it fixed upstream? Or am I missing
something and there's some documentation somewhere explaining what to do
in that case? (my fix is clearly not intended for users to do, I believe).

Thanks

Éric

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32597#comment:1>

Django

unread,
Mar 26, 2021, 6:46:16 PM3/26/21
to django-...@googlegroups.com
#32597: generic inline formsets: object has no attribute 'fk'
-------------------------------------+-------------------------------------
Reporter: etanter | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.2
Severity: Normal | Resolution:

Keywords: generic field, | Triage Stage:
inline formset | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

Hi, you should provide a minimal example to reproduce the problem and
confirm that the issue still exists on the master branch.

--
Ticket URL: <https://code.djangoproject.com/ticket/32597#comment:2>

Django

unread,
Mar 29, 2021, 2:35:54 AM3/29/21
to django-...@googlegroups.com
#32597: generic inline formsets: object has no attribute 'fk'
-------------------------------------+-------------------------------------
Reporter: Éric Tanter | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 2.2
Severity: Normal | Resolution: needsinfo

Keywords: generic field, | Triage Stage:
inline formset | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => needsinfo


--
Ticket URL: <https://code.djangoproject.com/ticket/32597#comment:3>

Reply all
Reply to author
Forward
0 new messages