{"title":"Form handling with class-based views","version":"3.2","locale":"es","docname":"topics/class-based-views/generic-editing","url":"/es/3.2/topics/class-based-views/generic-editing/","canonical":"https://djangodocs.dev/es/3.2/topics/class-based-views/generic-editing/","summary":"Form processing generally has 3 paths: Initial GET (blank or prepopulated form) POST with invalid data (typically redisplay form with errors) POST with valid data…","html":"<h1>Form handling with class-based views<a class=\"heading-anchor\" href=\"#form-handling-with-class-based-views\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Form processing generally has 3 paths:</p>\n<ul class=\"simple\">\n<li><p>Initial GET (blank or prepopulated form)</p></li>\n<li><p>POST with invalid data (typically redisplay form with errors)</p></li>\n<li><p>POST with valid data (process the data and typically redirect)</p></li>\n</ul>\n<p>Implementing this yourself often results in a lot of repeated boilerplate code\n(see <a class=\"reference internal\" href=\"/es/3.2/topics/forms/#using-a-form-in-a-view\"><span class=\"std std-ref\">Using a form in a view</span></a>). To help avoid\nthis, Django provides a collection of generic class-based views for form\nprocessing.</p>\n<section id=\"basic-forms\">\n<h2>Basic forms<a class=\"heading-anchor\" href=\"#basic-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Given a contact form:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">forms.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n\n<span class=\"k\">class</span> <span class=\"nc\">ContactForm</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>Form<span class=\"p\">):</span>\n    name <span class=\"o\">=</span> forms<span class=\"o\">.</span>CharField<span class=\"p\">()</span>\n    message <span class=\"o\">=</span> forms<span class=\"o\">.</span>CharField<span class=\"p\">(</span>widget<span class=\"o\">=</span>forms<span class=\"o\">.</span>Textarea<span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">send_email</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"c1\"># send email using the self.cleaned_data dictionary</span>\n        <span class=\"k\">pass</span>\n</code></pre></figure>\n<p>The view can be constructed using a <code class=\"docutils literal notranslate\">FormView</code>:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">views.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">myapp.forms</span> <span class=\"kn\">import</span> ContactForm\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.edit</span> <span class=\"kn\">import</span> FormView\n\n<span class=\"k\">class</span> <span class=\"nc\">ContactFormView</span><span class=\"p\">(</span>FormView<span class=\"p\">):</span>\n    template_name <span class=\"o\">=</span> <span class=\"s1\">&#39;contact.html&#39;</span>\n    form_class <span class=\"o\">=</span> ContactForm\n    success_url <span class=\"o\">=</span> <span class=\"s1\">&#39;/thanks/&#39;</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">form_valid</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> form<span class=\"p\">):</span>\n        <span class=\"c1\"># This method is called when valid form data has been POSTed.</span>\n        <span class=\"c1\"># It should return an HttpResponse.</span>\n        form<span class=\"o\">.</span>send_email<span class=\"p\">()</span>\n        <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>form_valid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n</code></pre></figure>\n<p>Notes:</p>\n<ul class=\"simple\">\n<li><p>FormView inherits\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin\" title=\"django.views.generic.base.TemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">TemplateResponseMixin</code></a> so\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.template_name\" title=\"django.views.generic.base.TemplateResponseMixin.template_name\"><code class=\"xref py py-attr docutils literal notranslate\">template_name</code></a>\ncan be used here.</p></li>\n<li><p>The default implementation for\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.form_valid\" title=\"django.views.generic.edit.FormMixin.form_valid\"><code class=\"xref py py-meth docutils literal notranslate\">form_valid()</code></a> simply\nredirects to the <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.success_url\" title=\"django.views.generic.edit.FormMixin.success_url\"><code class=\"xref py py-attr docutils literal notranslate\">success_url</code></a>.</p></li>\n</ul>\n</section>\n<section id=\"model-forms\">\n<h2>Model forms<a class=\"heading-anchor\" href=\"#model-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Generic views really shine when working with models.  These generic\nviews will automatically create a <a class=\"reference internal\" href=\"/es/3.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a>, so long as\nthey can work out which model class to use:</p>\n<ul class=\"simple\">\n<li><p>If the <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.ModelFormMixin.model\" title=\"django.views.generic.edit.ModelFormMixin.model\"><code class=\"xref py py-attr docutils literal notranslate\">model</code></a> attribute is\ngiven, that model class will be used.</p></li>\n<li><p>If <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object\" title=\"django.views.generic.detail.SingleObjectMixin.get_object\"><code class=\"xref py py-meth docutils literal notranslate\">get_object()</code></a>\nreturns an object, the class of that object will be used.</p></li>\n<li><p>If a <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.queryset\" title=\"django.views.generic.detail.SingleObjectMixin.queryset\"><code class=\"xref py py-attr docutils literal notranslate\">queryset</code></a> is\ngiven, the model for that queryset will be used.</p></li>\n</ul>\n<p>Model form views provide a\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.ModelFormMixin.form_valid\" title=\"django.views.generic.edit.ModelFormMixin.form_valid\"><code class=\"xref py py-meth docutils literal notranslate\">form_valid()</code></a> implementation\nthat saves the model automatically.  You can override this if you have any\nspecial requirements; see below for examples.</p>\n<p>You don’t even need to provide a <code class=\"docutils literal notranslate\">success_url</code> for\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/generic-editing/#django.views.generic.edit.CreateView\" title=\"django.views.generic.edit.CreateView\"><code class=\"xref py py-class docutils literal notranslate\">CreateView</code></a> or\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/generic-editing/#django.views.generic.edit.UpdateView\" title=\"django.views.generic.edit.UpdateView\"><code class=\"xref py py-class docutils literal notranslate\">UpdateView</code></a> - they will use\n<a class=\"reference internal\" href=\"/es/3.2/ref/models/instances/#django.db.models.Model.get_absolute_url\" title=\"django.db.models.Model.get_absolute_url\"><code class=\"xref py py-meth docutils literal notranslate\">get_absolute_url()</code></a> on the model object if available.</p>\n<p>If you want to use a custom <a class=\"reference internal\" href=\"/es/3.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a> (for instance to\nadd extra validation), set\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.form_class\" title=\"django.views.generic.edit.FormMixin.form_class\"><code class=\"xref py py-attr docutils literal notranslate\">form_class</code></a> on your view.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>When specifying a custom form class, you must still specify the model,\neven though the <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.form_class\" title=\"django.views.generic.edit.FormMixin.form_class\"><code class=\"xref py py-attr docutils literal notranslate\">form_class</code></a> may\nbe a <a class=\"reference internal\" href=\"/es/3.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a>.</p>\n</aside>\n<p>First we need to add <a class=\"reference internal\" href=\"/es/3.2/ref/models/instances/#django.db.models.Model.get_absolute_url\" title=\"django.db.models.Model.get_absolute_url\"><code class=\"xref py py-meth docutils literal notranslate\">get_absolute_url()</code></a> to our\n<code class=\"docutils literal notranslate\">Author</code> class:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">models.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n<span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> reverse\n\n<span class=\"k\">class</span> <span class=\"nc\">Author</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">200</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">get_absolute_url</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> reverse<span class=\"p\">(</span><span class=\"s1\">&#39;author-detail&#39;</span><span class=\"p\">,</span> kwargs<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s1\">&#39;pk&#39;</span><span class=\"p\">:</span> <span class=\"bp\">self</span><span class=\"o\">.</span>pk<span class=\"p\">})</span>\n</code></pre></figure>\n<p>Then we can use <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/flattened-index/#CreateView\" title=\"CreateView\"><code class=\"xref py py-class docutils literal notranslate\">CreateView</code></a> and friends to do the actual\nwork. Notice how we’re just configuring the generic class-based views\nhere; we don’t have to write any logic ourselves:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">views.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> reverse_lazy\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.edit</span> <span class=\"kn\">import</span> CreateView<span class=\"p\">,</span> DeleteView<span class=\"p\">,</span> UpdateView\n<span class=\"kn\">from</span> <span class=\"nn\">myapp.models</span> <span class=\"kn\">import</span> Author\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorCreateView</span><span class=\"p\">(</span>CreateView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Author\n    fields <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">]</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorUpdateView</span><span class=\"p\">(</span>UpdateView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Author\n    fields <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">]</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorDeleteView</span><span class=\"p\">(</span>DeleteView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Author\n    success_url <span class=\"o\">=</span> reverse_lazy<span class=\"p\">(</span><span class=\"s1\">&#39;author-list&#39;</span><span class=\"p\">)</span>\n</code></pre></figure>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>We have to use <a class=\"reference internal\" href=\"/es/3.2/ref/urlresolvers/#django.urls.reverse_lazy\" title=\"django.urls.reverse_lazy\"><code class=\"xref py py-func docutils literal notranslate\">reverse_lazy()</code></a> instead of\n<code class=\"docutils literal notranslate\">reverse()</code>, as the urls are not loaded when the file is imported.</p>\n</aside>\n<p>The <code class=\"docutils literal notranslate\">fields</code> attribute works the same way as the <code class=\"docutils literal notranslate\">fields</code> attribute on the\ninner <code class=\"docutils literal notranslate\">Meta</code> class on <a class=\"reference internal\" href=\"/es/3.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a>. Unless you define the\nform class in another way, the attribute is required and the view will raise\nan <a class=\"reference internal\" href=\"/es/3.2/ref/exceptions/#django.core.exceptions.ImproperlyConfigured\" title=\"django.core.exceptions.ImproperlyConfigured\"><code class=\"xref py py-exc docutils literal notranslate\">ImproperlyConfigured</code></a> exception if it’s not.</p>\n<p>If you specify both the <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.ModelFormMixin.fields\" title=\"django.views.generic.edit.ModelFormMixin.fields\"><code class=\"xref py py-attr docutils literal notranslate\">fields</code></a>\nand <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.form_class\" title=\"django.views.generic.edit.FormMixin.form_class\"><code class=\"xref py py-attr docutils literal notranslate\">form_class</code></a> attributes, an\n<a class=\"reference internal\" href=\"/es/3.2/ref/exceptions/#django.core.exceptions.ImproperlyConfigured\" title=\"django.core.exceptions.ImproperlyConfigured\"><code class=\"xref py py-exc docutils literal notranslate\">ImproperlyConfigured</code></a> exception will be raised.</p>\n<p>Finally, we hook these new views into the URLconf:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">urls.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.urls</span> <span class=\"kn\">import</span> path\n<span class=\"kn\">from</span> <span class=\"nn\">myapp.views</span> <span class=\"kn\">import</span> AuthorCreateView<span class=\"p\">,</span> AuthorDeleteView<span class=\"p\">,</span> AuthorUpdateView\n\nurlpatterns <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\"># ...</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;author/add/&#39;</span><span class=\"p\">,</span> AuthorCreateView<span class=\"o\">.</span>as_view<span class=\"p\">(),</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;author-add&#39;</span><span class=\"p\">),</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;author/&lt;int:pk&gt;/&#39;</span><span class=\"p\">,</span> AuthorUpdateView<span class=\"o\">.</span>as_view<span class=\"p\">(),</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;author-update&#39;</span><span class=\"p\">),</span>\n    path<span class=\"p\">(</span><span class=\"s1\">&#39;author/&lt;int:pk&gt;/delete/&#39;</span><span class=\"p\">,</span> AuthorDeleteView<span class=\"o\">.</span>as_view<span class=\"p\">(),</span> name<span class=\"o\">=</span><span class=\"s1\">&#39;author-delete&#39;</span><span class=\"p\">),</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>These views inherit\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin\"><code class=\"xref py py-class docutils literal notranslate\">SingleObjectTemplateResponseMixin</code></a>\nwhich uses\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix\"><code class=\"xref py py-attr docutils literal notranslate\">template_name_suffix</code></a>\nto construct the\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.template_name\" title=\"django.views.generic.base.TemplateResponseMixin.template_name\"><code class=\"xref py py-attr docutils literal notranslate\">template_name</code></a>\nbased on the model.</p>\n<p>In this example:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/flattened-index/#CreateView\" title=\"CreateView\"><code class=\"xref py py-class docutils literal notranslate\">CreateView</code></a> and <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/flattened-index/#UpdateView\" title=\"UpdateView\"><code class=\"xref py py-class docutils literal notranslate\">UpdateView</code></a> use <code class=\"docutils literal notranslate\">myapp/author_form.html</code></p></li>\n<li><p><a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/flattened-index/#DeleteView\" title=\"DeleteView\"><code class=\"xref py py-class docutils literal notranslate\">DeleteView</code></a> uses <code class=\"docutils literal notranslate\">myapp/author_confirm_delete.html</code></p></li>\n</ul>\n<p>If you wish to have separate templates for <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/flattened-index/#CreateView\" title=\"CreateView\"><code class=\"xref py py-class docutils literal notranslate\">CreateView</code></a> and\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/flattened-index/#UpdateView\" title=\"UpdateView\"><code class=\"xref py py-class docutils literal notranslate\">UpdateView</code></a>, you can set either\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-simple/#django.views.generic.base.TemplateResponseMixin.template_name\" title=\"django.views.generic.base.TemplateResponseMixin.template_name\"><code class=\"xref py py-attr docutils literal notranslate\">template_name</code></a> or\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix\" title=\"django.views.generic.detail.SingleObjectTemplateResponseMixin.template_name_suffix\"><code class=\"xref py py-attr docutils literal notranslate\">template_name_suffix</code></a>\non your view class.</p>\n</aside>\n</section>\n<section id=\"models-and-request-user\">\n<h2>Models and <code class=\"docutils literal notranslate\">request.user</code><a class=\"heading-anchor\" href=\"#models-and-request-user\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To track the user that created an object using a <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/flattened-index/#CreateView\" title=\"CreateView\"><code class=\"xref py py-class docutils literal notranslate\">CreateView</code></a>,\nyou can use a custom <a class=\"reference internal\" href=\"/es/3.2/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a> to do this. First, add\nthe foreign key relation to the model:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">models.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> User\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">Author</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    name <span class=\"o\">=</span> models<span class=\"o\">.</span>CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">200</span><span class=\"p\">)</span>\n    created_by <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>User<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n\n    <span class=\"c1\"># ...</span>\n</code></pre></figure>\n<p>In the view, ensure that you don’t include <code class=\"docutils literal notranslate\">created_by</code> in the list of fields\nto edit, and override\n<a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.ModelFormMixin.form_valid\" title=\"django.views.generic.edit.ModelFormMixin.form_valid\"><code class=\"xref py py-meth docutils literal notranslate\">form_valid()</code></a> to add the user:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"python\"><figcaption class=\"code-block-caption\"><code class=\"docutils literal notranslate\">views.py</code></figcaption>\n<div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.mixins</span> <span class=\"kn\">import</span> LoginRequiredMixin\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.edit</span> <span class=\"kn\">import</span> CreateView\n<span class=\"kn\">from</span> <span class=\"nn\">myapp.models</span> <span class=\"kn\">import</span> Author\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorCreateView</span><span class=\"p\">(</span>LoginRequiredMixin<span class=\"p\">,</span> CreateView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Author\n    fields <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">]</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">form_valid</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> form<span class=\"p\">):</span>\n        form<span class=\"o\">.</span>instance<span class=\"o\">.</span>created_by <span class=\"o\">=</span> <span class=\"bp\">self</span><span class=\"o\">.</span>request<span class=\"o\">.</span>user\n        <span class=\"k\">return</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>form_valid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n</code></pre></figure>\n<p><a class=\"reference internal\" href=\"/es/3.2/topics/auth/default/#django.contrib.auth.mixins.LoginRequiredMixin\" title=\"django.contrib.auth.mixins.LoginRequiredMixin\"><code class=\"xref py py-class docutils literal notranslate\">LoginRequiredMixin</code></a> prevents users who\naren’t logged in from accessing the form. If you omit that, you’ll need to\nhandle unauthorized users in <a class=\"reference internal\" href=\"/es/3.2/ref/class-based-views/mixins-editing/#django.views.generic.edit.ModelFormMixin.form_valid\" title=\"django.views.generic.edit.ModelFormMixin.form_valid\"><code class=\"xref py py-meth docutils literal notranslate\">form_valid()</code></a>.</p>\n</section>\n<section id=\"content-negotiation-example\">\n<span id=\"id1\"></span><h2>Content negotiation example<a class=\"heading-anchor\" href=\"#content-negotiation-example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Here is an example showing how you might go about implementing a form that\nworks with an API-based workflow as well as “normal” form POSTs:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> JsonResponse\n<span class=\"kn\">from</span> <span class=\"nn\">django.views.generic.edit</span> <span class=\"kn\">import</span> CreateView\n<span class=\"kn\">from</span> <span class=\"nn\">myapp.models</span> <span class=\"kn\">import</span> Author\n\n<span class=\"k\">class</span> <span class=\"nc\">JsonableResponseMixin</span><span class=\"p\">:</span>\n    <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Mixin to add JSON support to a form.</span>\n<span class=\"sd\">    Must be used with an object-based FormView (e.g. CreateView)</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n    <span class=\"k\">def</span> <span class=\"nf\">form_invalid</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> form<span class=\"p\">):</span>\n        response <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>form_invalid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span>request<span class=\"o\">.</span>accepts<span class=\"p\">(</span><span class=\"s1\">&#39;text/html&#39;</span><span class=\"p\">):</span>\n            <span class=\"k\">return</span> response\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> JsonResponse<span class=\"p\">(</span>form<span class=\"o\">.</span>errors<span class=\"p\">,</span> status<span class=\"o\">=</span><span class=\"mi\">400</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"nf\">form_valid</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> form<span class=\"p\">):</span>\n        <span class=\"c1\"># We make sure to call the parent&#39;s form_valid() method because</span>\n        <span class=\"c1\"># it might do some processing (in the case of CreateView, it will</span>\n        <span class=\"c1\"># call form.save() for example).</span>\n        response <span class=\"o\">=</span> <span class=\"nb\">super</span><span class=\"p\">()</span><span class=\"o\">.</span>form_valid<span class=\"p\">(</span>form<span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"bp\">self</span><span class=\"o\">.</span>request<span class=\"o\">.</span>accepts<span class=\"p\">(</span><span class=\"s1\">&#39;text/html&#39;</span><span class=\"p\">):</span>\n            <span class=\"k\">return</span> response\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            data <span class=\"o\">=</span> <span class=\"p\">{</span>\n                <span class=\"s1\">&#39;pk&#39;</span><span class=\"p\">:</span> <span class=\"bp\">self</span><span class=\"o\">.</span>object<span class=\"o\">.</span>pk<span class=\"p\">,</span>\n            <span class=\"p\">}</span>\n            <span class=\"k\">return</span> JsonResponse<span class=\"p\">(</span>data<span class=\"p\">)</span>\n\n<span class=\"k\">class</span> <span class=\"nc\">AuthorCreateView</span><span class=\"p\">(</span>JsonableResponseMixin<span class=\"p\">,</span> CreateView<span class=\"p\">):</span>\n    model <span class=\"o\">=</span> Author\n    fields <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;name&#39;</span><span class=\"p\">]</span>\n</code></pre></div>\n</section>","rootId":"form-handling-with-class-based-views","toc":[{"title":"Basic forms","anchor":"basic-forms","children":[]},{"title":"Model forms","anchor":"model-forms","children":[]},{"title":"Models and request.user","anchor":"models-and-request-user","children":[]},{"title":"Content negotiation example","anchor":"content-negotiation-example","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/es/3.2/topics/"},{"docname":"topics/class-based-views/index","title":"Class-based views","url":"/es/3.2/topics/class-based-views/"}],"prev":{"docname":"topics/class-based-views/generic-display","title":"Built-in class-based generic views","url":"/es/3.2/topics/class-based-views/generic-display/"},"next":{"docname":"topics/class-based-views/mixins","title":"Using mixins with class-based views","url":"/es/3.2/topics/class-based-views/mixins/"},"formats":{"html":"/es/3.2/topics/class-based-views/generic-editing/","markdown":"/es/3.2/topics/class-based-views/generic-editing.md","json":"/es/3.2/topics/class-based-views/generic-editing.json"},"source":"https://github.com/django/django/blob/stable/3.2.x/docs/topics/class-based-views/generic-editing.txt","official":"https://docs.djangoproject.com/es/3.2/topics/class-based-views/generic-editing/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}