Question: How can I display an AutoField in a ModelForm. I have a need
where a Form is generated and displayed. Based on the input, I need to
read the AutoField's value and search for it in my database.

My form:

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

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

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})

Relevant part of my template:

    <form class="" action="/clinic/checkin" method="post">
        {% csrf_token %}
        <div class="form-row">
            <div class="col-md-24 mb-6">
                <label for="cstid" id="lbl-cstid">Hospital ID</label>
                {{ form.cstid|add_class:"form-control" }}
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-24 mb-6">
                <label for="Name">Name</label>
                {{ form.name|add_class:"form-control" }}
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-2 mb-3">
                <label for="Age">Age</label>
                {{ form.age|add_class:"form-control" }}
            </div>
            <div class="col-md-4 mb-3">
                <label for="Age">Email</label>
                {{ form.email|add_class:"form-control" }}
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-12 mb-3">
                <label for="Mobile" id="lbl-Mobile">Mobile</label>
                {{ form.mobile|add_class:"form-control" }}
            </div>
            mobile
            <div class="col-md-12 mb-3">
                <label for="Alternate" id="lbl-Alternate">Alternate</label>
                {{ form.alternate|add_class:"form-control" }}
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-24">
                <label for="Address" id="lbl-Address">Address</label>
                {{ form.address|add_class:"form-control" }}
            </div>
            address
        </div>
        <div class="form-row">
            <div class="col-md-24">
                <label for="City" id="lbl-City">City</label>
                {{ form.city|add_class:"form-control" }}
            </div>
        </div>
        <div>
            <input class="btn btn-primary btton-right" value="Search"
id="btnCheckinSearch" type="submit">
        </div>
    </form>

The generated html:

    <form class="" action="/clinic/checkin" method="post">
        <input type="hidden" name="csrfmiddlewaretoken"
value="XYUgBCj11Vj5s65YvCvR3rsZBdEg990kkBstBmBlUce2TOINIVURk0Yw5X5pqCjJ">
        <div class="form-row">
            <div class="col-md-24 mb-6">
                <label for="cstid" id="lbl-cstid">Hospital ID</label>
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-24 mb-6">
                <label for="Name">Name</label>
                <input type="text" name="name" maxlength="35"
class="form-control" id="id_name" style="">
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-2 mb-3">
                <label for="Age">Age</label>
                <input type="number" name="age" class="form-control"
id="id_age">
            </div>
            <div class="col-md-4 mb-3">
                <label for="Age">Email</label>
                <input type="text" name="email" maxlength="50"
class="form-control" id="id_email">
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-12 mb-3">
                <label for="Mobile" id="lbl-Mobile">Mobile</label>
                <input type="text" name="mobile" maxlength="15"
class="form-control" id="id_mobile">
            </div>
            mobile
            <div class="col-md-12 mb-3">
                <label for="Alternate" id="lbl-Alternate">Alternate</label>
                <input type="text" name="alternate" maxlength="15"
class="form-control" id="id_alternate">
            </div>
        </div>
        <div class="form-row">
            <div class="col-md-24">
                <label for="Address" id="lbl-Address">Address</label>
                <input type="text" name="address" maxlength="80"
class="form-control" id="id_address">
            </div>
            address
        </div>
        <div class="form-row">
            <div class="col-md-24">
                <label for="City" id="lbl-City">City</label>
                <input type="text" name="city" maxlength="25"
class="form-control" id="id_city">
            </div>
        </div>
        <div>
            <input class="btn btn-primary btton-right" value="Search"
id="btnCheckinSearch" type="submit">
        </div>
    </form>

As you can see, there is not input field for cstid. In the actual
model, it's an automatic field. But in the ModelForm it needs to be a
form input so I can search for a patient's hospitalid (which is this
automatic field). How can I display this field in the form?

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 [email protected].
To post to this group, send email to [email protected].
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_-Ga9CZ0%3DUSbpqCuVUBVG_%3DV0auPcFH-O%2BbD7iye48tgQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to