Joel G Mathew
Joel G Mathew

Reputation: 8061

How do I make a field optional to enter, in a django ModelForm?

Scenario: I instantiate a ModelForm and pass it to a template which displays the form. When POST is submitted, code tries to search the database by any of the given inputs. I dont require all inputs to be entered as in the Model. I just need one (or more, if user desires to do an AND search) to be entered.

Question: How can I make any of the ModelForm fields optional, where in the Model, the field isnt optional. The field isnt optional in the Model because I have another ModelForm based on the same Model, where user is required to enter all his details.

My model:

class customer(models.Model):
    # Need autoincrement, unique and primary
    cstid = models.AutoField(primary_key=True, unique=True)
    name = models.CharField(max_length=35)
    age=models.IntegerField()
    gender_choices = (('male', 'Male'),
                            ('female', 'Female'))
    gender = models.CharField(
        choices=gender_choices, max_length=10, default='male')
    maritalstatus_choices = ( ('married', 'Married'),
                            ('unmarried', 'Unmarried'))
    maritalstatus = models.CharField(
        choices=maritalstatus_choices, max_length=10, default='Unmarried')
    mobile = models.CharField(max_length=15, default='')
    alternate = models.CharField(max_length=15, default='')
    email = models.CharField(max_length=50, default='', blank=True)
    address = models.CharField(max_length=80, default='', blank=True)
    city = models.CharField(max_length=25, default='', blank=True)
    occupation = models.CharField(max_length=25, default='', blank=True)
    bloodgroup_choices = (('apos', 'A+'),
        ('aneg', 'A-'),
        ('bpos', 'B+'),
        ('bneg', 'B-'),
        ('opos', 'O+'),
        ('oneg', 'O-'),
        ('abpos', 'AB+'),
        ('abneg', 'AB-'),
        ('unspecified', '-')
        )
    bloodgroup = models.CharField(choices=bloodgroup_choices, max_length=3, default='-', blank=True)
    class Meta:
        unique_together = ["name", "mobile", "age"]
    def __str__(self):
        return self.name

My form:

class CheckinPatientMetaForm(ModelForm):
    class Meta:
        model = customer
        exclude = [
            'gender',
            'maritalstatus',
            'occupation',
            'bloodgroup'
        ]

views.py:

def checkin_patient(request):
    results = ''
    if request.method == 'POST':
        form = CheckinPatientMetaForm(request.POST)
        print("POST data",request.POST)
    else:
        form = CheckinPatientMetaForm()
    return render(request, 'clinic/checkin.html', {'rnd_num': randomnumber(), 'form': form, 'SearchResults': results})

Upvotes: 1

Views: 2518

Answers (1)

JPG
JPG

Reputation: 88449

As @bdbd mentioned in comments, you can do it by specifying by required=False.

For example, if you want to age field to be optional, add it explicitly as

from django import forms


class CheckinPatientMetaForm(ModelForm):
    age = forms.IntegerField(required=False)

    class Meta:
        model = customer
        exclude = [
            'gender',
            'maritalstatus',
            'occupation',
            'bloodgroup'
        ]

Upvotes: 2

Related Questions