Sameer Atharkar
Sameer Atharkar

Reputation: 442

Display Database entry as option in a Django Form

I am trying to create an inventory app and one reservation app.

I am trying to take entries from the inventory model and one can reserve it with the help of reservation app.

What I am trying to achieve :

  1. The reservation app should take the entry of serial number from the inventory app serial number. It should either with the help of a drop down , which will show only the entry of specific serial number, so that random serial number should not be entered. ( I tried the concept of foreign key and many to many but I was unable to achieve it on the html page UI , but I achieved it on admin panel) Can you suggest what changes will be required ?

For now, I am creating a form like this : enter image description here

But it is very poor method, because form is not getting validated, I am redirecting user to other web page where there are serial numbers present.

I am trying to see how can we make the serial numbers available there itself as a drop down so that user can select which serial number to select.

  1. I am trying to add the slot field as unique entry. I tried to add the slot as unique = True in reservation_app/models.py but it is only working in admin panel. When I try to give the duplicate entry on html page, it is accepting the duplicate value and it is also getting stored in database.

I am trying to find its solution from very long via all over the internet, but it seems very difficult to me.

If anyone knows how it can be done. That will be extremely grateful. I am providing almost everything of the necessary code which can be required below:

inventory_app/models.py

from django.db import models
#from django.utils import timezone
# Create your models here.
class Form1(models.Model):
    item = models.CharField(max_length=125)
    quantity = models.IntegerField(default=0)
    vendor = models.CharField(max_length=125)
    inward = models.IntegerField(default=1234)
    sno = models.CharField(max_length=100)
    date = models.DateField()
    date_received = models.DateField()
    def __str__(self):
      return self.item

reservation_app/models.py

from django.db import models

# Create your models here.
class Reserve(models.Model):
   team = models.CharField(max_length=125 , default="DevOps" )
   rsno = models.CharField(max_length=125, default="ABCD12345")
   mac_address = models.CharField(max_length=125 , default="AA-00-04-00-XX-YY")
   hostname = models.CharField(max_length=125, default="gitlab.altiostar.com")
   ue = models.CharField(max_length=125 , null=True)
   slot = models.DateField(null=True)
   def __str__(self):
    return self.team

reservation_app/views.py

from django.shortcuts import render, HttpResponse
from .models import Reserve 
#from reservation_app.models import Reserve


# Create your views here.
def reserve(request):
    if request.method == "POST":
        team = request.POST.get('team')
        rsno = request.POST.get('rsno')
        mac_address = request.POST.get('mac_address')
        hostname = request.POST.get('hostname')
        ue = request.POST.get('ue')
        slot = request.POST.get('slot')
        reserve = Reserve( team=team , rsno=rsno, slot=slot , mac_address = mac_address , hostname = hostname , ue = ue ) #security_stamp_date=security_stamp_date 
        reserve.save()
    return render(request, 'home.html')

inventory_app/views.py

from django.shortcuts import render, HttpResponse
from inventory_app.models import Form1
import datetime
from .models import Form1 
import csv
from .filters import ItemFilter

def form1(request):
    if request.method == "POST":
        item = request.POST.get('item')
        quantity = request.POST.get('quantity')
        sno = request.POST.get('sno')
        inward = request.POST.get('inward')
        vendor = request.POST.get('vendor')
        date_received = request.POST.get('date_received')
     #   security_stamp_date = request.POST.get('security_stamp_date')
        form1 = Form1(item=item, quantity=quantity , inward=inward , sno=sno , vendor=vendor, date=datetime.datetime.now() , date_received=date_received ) #security_stamp_date=security_stamp_date 
        form1.save()

    return render(request, 'form1.html')

home.html page of reservation_app

<div class="form-group">
          <label for="exampleFormControlInput1">Radio Serial Number</label>
          <input type="text" class="form-control" id="exampleFormControlInput1" name= "rsno" placeholder="S/N123ABC123" required >
        </div>
        <a href="http://127.0.0.1:8000/form1_entries/"  target="_blank" class="btn btn-info btn-sm" role="button" aria-disabled="true"> Check Available Serial Numbers </a>

      <div class="form-group">
        <label for="exampleFormControlInput1">Book your slot for Date</label>
        <input type="datetime-local" class="form-control" id="exampleFormControlInput1" name= "slot" >
      </div>

      <button type="submit" class="btn btn-primary"> Submit </button>

form1.html of inventory_app

    <div class="form-group">
      <label for="exampleFormControlInput1">Serial Number</label>
      <input type="text" class="form-control" id="exampleFormControlInput1" name= "serial_number" placeholder="S/N123ABC123" required >
    </div>

      <button type="submit" class="btn btn-primary"> Submit </button>

Thanks for your time :)

Upvotes: 1

Views: 368

Answers (1)

hendrikschneider
hendrikschneider

Reputation: 1846

  1. What you want to do is to create a dynamic form where you generate a choice field. Check out this article about creating a dynamic form: https://www.caktusgroup.com/blog/2018/05/07/creating-dynamic-forms-django/

Upvotes: 1

Related Questions