简体   繁体   English

在 Django 表单中将数据库条目显示为选项

[英]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.我正在尝试从库存 model 中获取条目,并且可以在预订应用程序的帮助下进行预订。

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? (我尝试了外键和多对多的概念,但我无法在 html 页面 UI 上实现它,但我在管理面板上实现了它)您能否建议需要进行哪些更改?

For now, I am creating a form like this:现在,我正在创建一个这样的表单: 在此处输入图像描述

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.但这是非常糟糕的方法,因为表单没有得到验证,我将用户重定向到其他存在序列号的 web 页面。

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.我正在尝试查看我们如何将序列号作为下拉列表提供,以便用户可以将 select 序列号设为 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.我尝试在 reservation_app/models.py 中将插槽添加为 unique = True ,但它仅在管理面板中工作。 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.当我尝试在 html 页面上提供重复条目时,它正在接受重复值并且它也被存储在数据库中。

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库存应用程序/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预订应用程序/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预订应用程序/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库存应用程序/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 home.html 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 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:)谢谢你的时间:)

  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/查看这篇关于创建动态表单的文章: https://www.caktusgroup.com/blog/2018/05/07/creating-dynamic-forms-django/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:如何根据表单中的选定选项在模板上显示来自 django 数据库的图像? - Django: how can i display an image from the django database on a template based on an selected option from a form? 在Django中显示数据格式的mysql数据库 - Display data form mysql database in Django 如何在数据库中获取一个值并在 django 表单上显示 - How to get a value in a database and display on django form Django:参考数据库条目 - Django: Reference Database Entry django 1.8-如果表单条目查询结果与数据库不匹配,则在同一页面上显示警告消息,而不是“无”或引发异常页面 - django 1.8- if form entry query result does't match database, display alert message on same page, instead of “None” or raise exception page Django表单不会覆盖先前的条目 - Django form not overwriting previous entry 模板中的Django格式数据库条目 - Django format database entry in template Django-延迟创建数据库条目 - Django - Delay in creating database entry Django Admin:如何在不打开条目更改表的情况下从list_display本身更新/更改图像 - Django Admin : How to update/change the image from the list_display itself without opening the change form for the entry 如何从表单提交中获取输入并使用它来显示使用 django 的条目 - how to take input from form submission and use it to display an entry using django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM