forms.models.BaseModelFormSet.changed_objects

models.BaseModelFormSet.changed_objects

forms.models.BaseModelFormSet

class models.BaseModelFormSet Like regular formsets, Django provides a couple of enhanced formset classes that make it easy to work with Django models. Let’s reuse the Author model from above: >>> from django.forms import modelformset_factory >>> from myapp.models import Author >>> AuthorFormSet = modelformset_factory(Author, fields=('name', 'title')) Using fields restricts the formset to use only the given fields. Alternatively, you can take an “opt-out” approach

forms.models.BaseInlineFormSet

class models.BaseInlineFormSet Inline formsets is a small abstraction layer on top of model formsets. These simplify the case of working with related objects via a foreign key. Suppose you have these two models: from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): author = models.ForeignKey(Author, on_delete=models.CASCADE) title = models.CharField(max_length=100) If you want to create a formset that allows

forms.ModelMultipleChoiceField.to_field_name

to_field_name Same as ModelChoiceField.to_field_name.

forms.ModelMultipleChoiceField.queryset

queryset Same as ModelChoiceField.queryset. Takes one optional argument:

forms.ModelMultipleChoiceField

class ModelMultipleChoiceField(**kwargs) [source] Default widget: SelectMultiple Empty value: An empty QuerySet (self.queryset.none()) Normalizes to: A QuerySet of model instances. Validates that every id in the given list of values exists in the queryset. Error message keys: required, list, invalid_choice, invalid_pk_value The invalid_choice message may contain %(value)s and the invalid_pk_value message may contain %(pk)s, which will be substituted by the appropriate values. Allows the

forms.ModelForm

class ModelForm [source] If you’re building a database-driven app, chances are you’ll have forms that map closely to Django models. For instance, you might have a BlogComment model, and you want to create a form that lets people submit comments. In this case, it would be redundant to define the field types in your form, because you’ve already defined the fields in your model. For this reason, Django provides a helper class that lets you create a Form class from a Django model. For example: &

forms.ModelChoiceField.to_field_name

to_field_name This optional argument is used to specify the field to use as the value of the choices in the field’s widget. Be sure it’s a unique field for the model, otherwise the selected value could match more than one object. By default it is set to None, in which case the primary key of each object will be used. For example: # No custom to_field_name field1 = forms.ModelChoiceField(queryset=...) would yield: <select id="id_field1" name="field1"> <option value="obj1.pk">Obje

forms.ModelChoiceField.queryset

queryset A QuerySet of model objects from which the choices for the field will be derived, and which will be used to validate the user’s selection. ModelChoiceField also takes two optional arguments:

forms.ModelChoiceField.empty_label

empty_label By default the <select> widget used by ModelChoiceField will have an empty choice at the top of the list. You can change the text of this label (which is "---------" by default) with the empty_label attribute, or you can disable the empty label entirely by setting empty_label to None: # A custom empty label field1 = forms.ModelChoiceField(queryset=..., empty_label="(Nothing)") # No empty label field2 = forms.ModelChoiceField(queryset=..., empty_label=None) Note that if a