Django's ModelChoiceField
is the default form field used for foreign keys when deriving a form from a model using ModelForm
. Upon validation, the field will check that selected value does exist in the corresponding related table, and raise a ValidationError
if it is not the case.
I'm creating a form for a Document
model that has a type
field, a foreign key to a Type
model which does only contain a name
attribute. Here is the code of models.py
for clarity
class Type(models.Model):
name = models.CharField(max_length=32)
class Document(models.Model):
title = models.CharField(max_length=256)
type = models.ForeignKey(Type, related_name='related_documents')
Instead of a standard select
control, I'm using selectize.js to provide auto-completion to the users. Moreover, selectize provides a "create" option, that allows to enter a value that does not exist yet into the select.
I would like to extend the ModelChoiceField
in order to create a new Type
object when the selected value does not exist (the new value will be assigned to name
field, this should be an option of the field for reusability). If possible, I would like the object to not be inserted into DB until save()
is called on the validated form (to prevent that multiple failed validation create multiple rows in db). What would be a good way to do so in Django? I tried to look into the documentation and the source code, tried to override ModelChoiceField
and tried to build this behavior basted on a TextField
but I'm not sure if there isn't a simpler way to do it.
I looked into the following questions but couldn't find the answer.
- Django ModelChoiceField has no plus button
- Django: override RelatedFieldWidgetWrapper
- How to set initial value in a dynamic Django ModelChoiceField
I would like to keep the process of adding new types as simple as possible - i.e.: do not use a pop-up attached to a '+' button. User should be able to type the value, and the value gets created if it doesn't exist.
Thanks