djangodocs.org

654 sections in Django 4.2 Search all versions →

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.

can_order

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.

can_order

Topic guide Formsets Dealing with ordering and deletion of forms

formset = ArticleFormSet( ... data, ... initial=[ ... {"title": "Article #1", "pub_date": datetime.date(2008, 5, 10)}, ... {"title": "Article #2", "pub_date": datetime.date(2008, 5, 11)}, ... ], ... ) formset.is_valid() True for form in formset.ordered_forms: ... print(form.cleaned_data) ...

can_delete

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.

can_delete

Topic guide Formsets Dealing with ordering and deletion of forms

If you are using a ModelFormSet, model instances for deleted forms will be deleted when you call formset.save(). If you call formset.save(commit=False), objects will not be deleted automatically.

Topic guide Formsets

BaseFormSet.as_div() : > New in Django 4.1. 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 …

Field types

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.

A full example

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.):

Model formsets

Topic guide Creating forms from models

classmodels.BaseModelFormSet Like regular formsets, Django provides a couple of enhanced formset classes to make working with Django models more convenient. Let’s reuse the Author model from above: Using fields restricts the formset to use only the given fields.

Topic guide Creating forms from models Model formsets

As with a ModelForm, you can save the data as a model object. This is done with the formset’s save() method: The save() method returns the instances that have been saved to the database.

Setting test cookies

Topic guide How to use sessions

As a convenience, Django provides a way to test whether the user’s browser accepts cookies. Call the set_test_cookie() method of request.session in a view, and call test_cookie_worked() in a subsequent view – not in the same view call.

Topic guide Format localization

When formatting is enabled, Django can use localized formats when parsing dates, times and numbers in forms. That means it tries different formats for different locales when guessing the format used by the user when inputting data on forms.

Definitions

Topic guide Internationalization and localization

Usually done by developers. localization Writing the translations and local formats. Usually done by translators. More details can be found in the W3C Web Internationalization FAQ, the Wikipedia article or the GNU gettext documentation.

Code

Topic guide Time zones Migration guide

Since Django now gives you aware datetimes, you’ll get exceptions wherever you compare a datetime that comes from a model or a form with a naive datetime that you’ve created in your code.