18👍
Presuming you want to make last_name
optional, you can use the blank
attribute:
class Student(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40, blank=True)
email = models.EmailField()
Note that on CharField
and TextField
, you probably don’t want to set null
(see this answer for a discussion as to why), but on other field types, you’ll need to, or you’ll be unable to save instances where optional values are omitted.
18👍
You use the required
argument, sent in with a False
value:
email = models.EmailField(required=False)
- Django : Formset as form field
- How can I automatically let syncdb add a column (no full migration needed)
- Django templates: Convert float to integer if it ends with .0?
- How to add attributes to option tags?
5👍
If you want to allow blank values in a date field (e.g., DateField
, TimeField
, DateTimeField
) or numeric field (e.g., IntegerField
, DecimalField
, FloatField
), you’ll need to use both null=True
and blank=True
.
- How to get rid of the #_=_ in the facebook redirect of django-social-auth?
- Override Django allauth signup "next" redirect URL
-5👍
class StudentForm(ModelForm):
class Meta:
model = Student
exclude = ['first_name', ...]
- Django runserver error when specifying port
- GeoDjango, difference between dwithin and distance_lt?
- Custom unique_together key name
- SQL injection hacks and django
Source:stackexchange.com