I am building a Django website from a tutorial. The model is called Post and looks like this:
class Post(models.Model):
author = models.ForeignKey('auth.User', verbose_name='Author')
title = models.CharField(max_length=200, verbose_name='Titel')
text = HTMLField()
created_date = models.DateTimeField(default=timezone.now, verbose_name='Erstellungsdatum')
published = models.BooleanField(blank=True, verbose_name='Veröffentlichung')
tags = models.ManyToManyField(Tag, blank=True)
class Meta:
verbose_name = 'Artikel'
verbose_name_plural = 'Artikels'
ordering = ['-created_date']
def publish(self):
self.published = True
self.save()
def __str__(self):
return self.title
as you can see, i made some fields blank=True
.
The migration/sql Django is now performing looks like this:
ALTER TABLE "blog_post" RENAME TO "blog_post__old";
CREATE TABLE "blog_post" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"published" bool NOT NULL, "title" varchar(200) NOT NULL, "text" text NOT
NULL, "created_date" datetime NOT NULL, "author_id" integer NOT NULL
REFERENCES "auth_user" ("id"));
INSERT INTO "blog_post" ("id", "title", "text", "created_date", "author_id",
"published") SELECT "id", "title", "text", "created_date", "author_id",
"published" FROM "blog_post__old";
DROP TABLE "blog_post__old";
CREATE INDEX "blog_post_author_id_dd7a8485" ON "blog_post" ("author_id");
COMMIT;
It just ignores the blank-values and makes every field NOT NULL. i saw this also in this question
The problem is, that if i implement the form in the template this field is "required" input, so the form cannot be submitted if the checkbox is unchecked.
Thanks in advance!
forms.py
from django import forms
from tinymce.widgets import TinyMCE
from .models import Post
class PostForm(forms.ModelForm):
text = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30, 'class': 'materialize-textarea'}))
published = forms.BooleanField(widget=forms.CheckboxInput())
class Meta:
model = Post
fields = ('title', 'published', 'text', 'tags')
template
{% extends 'blog/base.html' %}
{% block content_block %}
<div class="row">
<h1>Edit Post</h1>
</div>
<div class="row">
<form class="col s12" action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="waves-effect waves-light btn"><i class="material-icons left">save</i>Speichern</button>
</form>
</div>
{% endblock %}
published = models.BooleanField(blank=True, verbose_name='Veröffentlichung')
source refer the docs django model references
Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required.
In your forms
published = forms.BooleanField(required=False,widget=forms.CheckboxInput())
Source Requred filed
Hope its working
The BooleanField
can store True
or False
, so having NOT NULL
isn't a problem.
If you really want to allow null
for a field, then you need null=True
. This is a separate option from blank=True
which you already have.
You shouldn't need to override the boolean field in your model form, since it is already a checkbox. However, if you do override it, then you need to set required=False
if you want the field to be optional.
published = forms.BooleanField(widget=forms.CheckboxInput(), required=False)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.