In django how to make form field optional ?
my model,
class Student(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
In django how to make form field optional ?
my model,
class Student(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
You use the
required
argument, sent in with aFalse
value:email = models.EmailField(required=False)
Presuming you want to make
last_name
optional, you can use theblank
attribute:Note that on
CharField
andTextField
, you probably don't want to setnull
(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.Use
null=True
andblank=True
in your model.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 bothnull=True
andblank=True
.