You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
classJobDuplicateView(JobCreateView):
defget_context_data(self, **kwargs):
data=super().get_context_data(**kwargs)
cloned_constraints=self.duplicated_constraints# Prepopulate the formset with the cloned constraintsinitial= [{} for_incloned_constraints]
fori, constraintinenumerate(cloned_constraints):
constraint_form=data['constraint_formset'].get_form_class(constraint.__class__)()
ct=ContentType.objects.get_for_model(constraint)
initial[i]['polymorphic_ctype'] =ctforname, fieldinconstraint_form.fields.items():
ifname!="id"andname!="job":
initial[i][name] =getattr(constraint, name)
data['constraint_formset'].initial=initialdata['constraint_formset'].extra=len(cloned_constraints)
returndata
The text was updated successfully, but these errors were encountered:
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()
Use the wrapper for initial. Using your snippet above:
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:
classCustomBasePolymorphicInlineFormSet(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")
ifisinstance(ctype, ContentType):
form.initial["polymorphic_ctype"] =ctype.idreturnform
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.
The text was updated successfully, but these errors were encountered: