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

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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 via email to