Open michaelbaisch opened 1 year ago
I've run into this as well, and I used this as a workaround:
Create a wrapper class for ContentType
class CTWrap:
''' Wrapper for ContentType to work around a bug in polymorphic inlineformsets'''
def __init__(self,*args,**kwargs):
self.ct = args[0]
self.id = self.ct.id
def __str__(self):
return str(self.id)
def model_class(self):
return self.ct.model_class()
ct = ContentType.objects.get_for_model(constraint)
ctw = CTWrap(ct)
initial[i]['polymorphic_ctype'] = ctw
The logic being: whatever causes this bug is using the str of the ContentType instead of the id when generating the form. The wrapper overrides str to return the id, which then gets into the form instead, and then the POST processing works. The wrapper also has the id directly on it and the model_class method which are the only other parts of ContentType being used here.
@joshbaran looks interesting. My workaround is a custom subclass for BasePolymorphicInlineFormSet
:
class CustomBasePolymorphicInlineFormSet(BasePolymorphicInlineFormSet):
def _construct_form(self, i, **kwargs):
"""
Workaround:
For 'initial' data 'polymorphic_ctype' is expected
to be a ContentType object. However, in the form, it must be the id as int.
This method ensures that the ContentType object is converted to id after is was used.
"""
form = super()._construct_form(i, **kwargs)
ctype = form.initial.get("polymorphic_ctype")
if isinstance(ctype, ContentType):
form.initial["polymorphic_ctype"] = ctype.id
return form
When trying to populate a polymorphic inline formset using the django-polymorphic package, I'm encountering a problem with the
polymorphic_ctype
field.Context
In the
get_context_data
method, I populate the formset's initial data using:This works for an initial GET request to display the form. However, upon a POST, the formset becomes populated with the POST data where
polymorphic_ctype
is a string (eg "scheduler | sun separation constraint") and not the expected id, leading to an error:["Formset row constraint_set-0 has no 'polymorphic_ctype' defined!"]
Expected Behavior
The
polymorphic_ctype
should be correctly recognized.Current Behavior
The code expects an instance of ContentType:
However, it also tries to retrieve an id from the POST data, which causes the inconsistency:
Django version: 4.2.4 django-polymorphic version: 3.1.0 Python version: 3.11.5
Is there a better method to populate the formset? I'm attempting this approach to implement a "Duplication" feature. The intention is for the form to display data from the original Job and its associated constraints. This data has not been saved yet, allowing the user to make any necessary corrections.