Topic guide
Using mixins with class-based views
Avoid anything more complex
The number of subtle interactions between FormMixin and DetailView is already testing our ability to manage things. It’s unlikely you’d want to write this kind of class yourself.
Topic guide
Using mixins with class-based views
Avoid anything more complex
So why not do just that? We have a very clear division here: GET requests should get the DetailView (with the Form added to the context data), and POST requests should get the FormView. Let’s set up those views first.
Topic guide
Models
Fields
help_text : Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. primary_key : If True, this field is the primary key for the model.
Topic guide
Models
FieldsRelationships
… you are using an intermediate model, you can also query on its attributes: If you need to access a membership’s information you may do so by directly querying the Membership model: Another way to access the same information is by …
Topic guide
Multiple databases
Django’s admin doesn’t have any explicit support for multiple databases.
Topic guide
Making queries
Retrieving objects
Field lookups are how you specify the meat of an SQL WHERE clause. They’re specified as keyword arguments to the QuerySet methods filter(), exclude() and get(). Basic lookups keyword arguments take the form field__lookuptype=value. (That’s a double-underscore).
Topic guide
Making queries
Retrieving objects
… equivalent: The use of pk isn’t limited to __exact queries – any query term can be combined with pk to perform a query on the primary key of a model: pk lookups also work across joins. For example, these three …
Topic guide
Making queries
Querying JSONField
… To query a key for JSON null, None or JSONNull() can be used. Multiple keys can be chained together to form a path lookup: If the key is an integer, it will be interpreted as an index transform in an …
Topic guide
Formsets
Initial data is what drives the main usability of a formset. As shown above you can define the number of extra forms.
Topic guide
Formsets
The absolute_max parameter to formset_factory() allows limiting the number of forms that can be instantiated when supplying POST data. This protects against memory exhaustion attacks using forged POST requests: When absolute_max is None, it defaults to max_num + 1000.
Topic guide
Formsets
Dealing with ordering and deletion of forms
BaseFormSet.can_order Default: False Lets you create a formset with the ability to order: This adds an additional field to each form. This new field is named ORDER and is an forms.IntegerField.
Topic guide
Formsets
Dealing with ordering and deletion of forms
BaseFormSet.can_delete Default: False Lets you create a formset with the ability to select forms for deletion: Similar to can_order this adds a new field to each form named DELETE and is a forms.BooleanField.
Topic guide
Formsets
Dealing with ordering and deletion of forms
If you call formset.save(commit=False), objects will not be deleted automatically.
Topic guide
Formsets
If you need to add additional fields to the formset this can be easily accomplished. The formset base class provides an add_fields method.
Topic guide
Formsets
BaseFormSet.as_div() : Renders the formset with the template_name_div template. BaseFormSet.as_p() : Renders the formset with the template_name_p template. BaseFormSet.as_table() : Renders the formset with the template_name_table template. BaseFormSet.as_ul() : Renders the formset with the template_name_ul template.
Topic guide
Working with forms
Forms in Django
… the database, for example) pass it to the template context expand it to HTML markup using template variables Rendering a form in a template involves nearly the same work as rendering any other kind of object, but there are some …
Topic guide
Creating forms from models
classModelForm[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.
Topic guide
Creating forms from models
ModelForm
As you might expect, the ForeignKey and ManyToManyField model field types are special cases: ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet.
Topic guide
Creating forms from models
ModelForm
Consider this set of models: With these models, the ModelForm subclasses above would be roughly equivalent to this (the only difference being the save() method, which we’ll discuss in a moment.):
Topic guide
Creating forms from models
ModelForm
You can create forms from a given model using the standalone function modelform_factory(), instead of using a class definition.