UnsupportedChildType when using formsets? #812
Replies: 3 comments
-
|
I had the same issue as I forgot to include the child class (StackedPolymorphicInline.Child) in the inline (StackedPolymorphicInline). |
Beta Was this translation helpful? Give feedback.
-
|
@bckohan i think this is a user error not a bug in the library. I believe we can close this |
Beta Was this translation helpful? Give feedback.
-
|
That error is basically saying:
Why it “worked the first time” The fix: always filter the queryset to the child type for that formsetEven though you create the factory like this, the filtering is not “baked in”: ShirtsAndBlouseForm = polymorphic_modelformset_factory(
Product,
fields=product_fields,
formset_children=(
PolymorphicFormSetChild(ShirtsAndBlouse, fields=product_fields + [...]),
)
)You must do this when instantiating the formset: # GET
formset = ShirtsAndBlouseForm(
queryset=Product.objects.instance_of(ShirtsAndBlouse)
)
# POST (must use the same queryset concept!)
formset = ShirtsAndBlouseForm(
request.POST,
request.FILES,
queryset=Product.objects.instance_of(ShirtsAndBlouse)
)And similarly: formset = PhonesTabsForm(
queryset=Product.objects.instance_of(PhoneTablets)
)
If you’re using the formset only to create new itemsUse an empty queryset so it never tries to render existing objects of other types: formset = ShirtsAndBlouseForm(queryset=Product.objects.none())Alternative: use one formset that registers all child typesIf your UI is “one page that can edit mixed products”, then you should register all children in a single formset, and only display the subset you want in the template (or via initial client-side filtering). The library’s rule is: every model instance in the queryset must have a registered child form. Here's a checklist
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a polymorphic model that I made and it has many child models which I want the users to be choosing dynamically, posting the first product has no issue, I was able to select the form to send to the user, however when I try subsequent queries for the model to send to user I get error
polymorphic.formsets.models.UnsupportedChildType: The 'ShirtsAndBlouseForm' found a 'PhoneTablets' model in the queryset, but no form class is registered to display it.my forms look like this
I dont understand what could be happening that it can allow me to select the form during the first time but not the second time when product exist in the database
Beta Was this translation helpful? Give feedback.
All reactions