121👍
Guess your model is like this:
class My_Class(models.Model):
address = models.CharField()
Your form for Django version < 1.8:
class My_Form(ModelForm):
address = forms.CharField(required=False)
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
Your form for Django version > 1.8:
class My_Form(ModelForm):
address = forms.CharField(blank=True)
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
143👍
class My_Form(forms.ModelForm):
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
def __init__(self, *args, **kwargs):
super(My_Form, self).__init__(*args, **kwargs)
self.fields['address'].required = False
- [Django]-Creating email templates with Django
- [Django]-Storing an Integer Array in a Django Database
- [Django]-OneToOneField() vs ForeignKey() in Django
29👍
field = models.CharField(max_length=9, default='', blank=True)
Just add blank=True
in your model field and it won’t be required when you’re using modelforms
.
"If the model field has blank=True
, then required is set to False on the form field. Otherwise, required=True
."
source:
https://docs.djangoproject.com/en/4.1/topics/forms/modelforms/#field-types
[Edit]: Change django doc link from 3.1 to 4.1
- [Django]-Django index page best/most common practice
- [Django]-Check if celery beat is up and running
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-What is actually assertEquals in Python?
- [Django]-How to filter objects for count annotation in Django?
- [Django]-Embed YouTube video – Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
6👍
Solution: use both blank=True
, null=True
.
my_field = models.PositiveIntegerField(blank=True, null=True)
Explanation:
If you use null=True
my_field = models.PositiveIntegerField(null=True)
then my_field
is required, with *
next to it in the form and you can’t submit the empty value.
If you use blank=True
my_field = models.PositiveIntegerField(blank=True)
then my_field
is not required, there won’t be a *
next to it in the form and you can’t submit the value. But it will get null field not allowed.
Note: marking as not required and allowing null fields are two different things.
Pro Tip: Read the error more carefully than documentation.
- [Django]-Best way to integrate SqlAlchemy into a Django project
- [Django]-Django: How to check if the user left all fields blank (or to initial values)?
- [Django]-You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application
5👍
@Anentropic’s solution from the comment on @Atma’s answer worked for me. And I think it’s the best one too.
His comment:
null=True, blank=True
will cause theModelForm
field to berequired=False
I just set it on my ManyToMany field in my UserProfile
class and it worked flawlessly.
My UserProfile
class now looks like this (notice the friends
field):
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
friends = models.ManyToManyField('self', null=True, blank=True)
I also think that this is the most beautiful solution since you do the same thing, put null
and blank
to True
, weather you have a simple char
field or, like I have, ManyToMany
field.
- [Django]-How do you Serialize the User model in Django Rest Framework
- [Django]-How to save pillow image object to Django ImageField?
- [Django]-AttributeError: 'module' object has no attribute 'tests'
0👍
The above answers are correct; nevertheless due note that setting null=True
on a ManyToManyField has no effect at the database level and will raise the following warning when migrating:
(fields.W340) null has no effect on ManyToManyField.
A good answer to this is explained in this other thread.
- [Django]-ImportError: cannot import name '…' from partially initialized module '…' (most likely due to a circular import)
- [Django]-Django content-type : how do I get an object?
- [Django]-In Django, how do I check if a user is in a certain group?