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

608 views
Skip to first unread message

Joel Mathew

unread,
Sep 12, 2018, 6:14:41 PM9/12/18
to django...@googlegroups.com
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})

Joel G Mathew

Ryan Gedwill

unread,
Sep 12, 2018, 7:30:45 PM9/12/18
to django...@googlegroups.com
There may be a better way, but I’d set ‘required=False’ for the field.

Then on the template where it is actually required, hardcode validation logic in either JS or with the template tags, by simply checking if the field is empty and displaying an error if it is empty, and submitting the form if it is not.

You could also do it the OOP way by inheriting your modelform with a new modelform, and overriding the field for the new one.

For example in the regular modelform set required=True, then in the inherited version set required=False.

I’d do it the JS/template tag way, and I most likely have done it before.


Sent from my iPhone
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA%3Diw_9CQJSKzq7EbJnRVBZHbyDF0Nmx7TidBymxJROtY-XfrQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages