Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django how to make form fields optional

Tags:

python

django

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()
like image 438
Nishant Avatar asked Apr 24 '14 12:04

Nishant


People also ask

How do you make a field not required in Django?

The simplest way is by using the field option blank=True (docs.djangoproject.com/en/dev/ref/models/fields/#blank).

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

How do you make a field not editable in Django?

disabled. The disabled boolean argument, when set to True , disables a form field using the disabled HTML attribute so that it won't be editable by users. Even if a user tampers with the field's value submitted to the server, it will be ignored in favor of the value from the form's initial data.

How do you define choice fields in Django?

Django Field Choices. According to documentation Field Choices are a sequence consisting itself of iterables of exactly two items (e.g. [(A, B), (A, B) …]) to use as choices for some field. For example, consider a field semester which can have options as { 1, 2, 3, 4, 5, 6 } only.


1 Answers

You use the required argument, sent in with a False value:

email = models.EmailField(required=False)

like image 136
Marshall X Avatar answered Oct 12 '22 23:10

Marshall X