[Django]-Django Forms for generic relationships. How to include them?

1👍

If you use a model form you can see what kind of form elements they use.

For content_type it will typically use a ModelChoiceField with ContentType.objects.all() as the queryset, and object_id will be a TextInput looking for the positive integer. I don’t think the actual generic accessor will show up in the form if you use a model form.

If you want a more elegant solution than this I’d look into writing a custom form field or widget to handle it.

I don’t think it’s bad practice to add additional fields to the ModelForm as you have, in fact I think that’s a good route to go about it. Overriding the save method will probably give you the functionality you desire.

Leave a comment