[英]How to auto populate a field in django when a value is selected from dropdown menu in another field in forms?
I have two models that are located in two different apps;我有两个模型位于两个不同的应用程序中; Invoice and Inventory .
发票和库存。
InvoiceModel:发票型号:
class Invoice(models.Model):
line_one = models.ForeignKey(Inventory, to_field='title', on_delete=models.CASCADE, default='', blank=True, null=True, related_name='+', verbose_name="Line 1")
line_one_unit_price = models.IntegerField('Unit Price(₹)', default=0, blank=True, null=True)
line_one_quantity = models.IntegerField('Quantity', default=0, blank=True, null=True)
line_one_total_price = models.IntegerField('Line Total', default=0, blank=True, null=True)
Invoice.line_one is referenced to Inventory.title Invoice.line_one引用到Inventory.title
InventoryModel :库存模型:
class Inventory(models.Model):
product_number = models.IntegerField(blank=True, null=True)
product = models.TextField(max_length=3000, default='', blank=True, null=True)
title = models.CharField('Title', max_length=120, default='', blank=True, null=True, unique=True)
amount = models.IntegerField('Unit Price', default=0, blank=True, null=True)
def __str__(self):
return self.title
So basically the user adds a product using the Inventory model and then in the Invoice model, they'll have a drop-down menu for line_one and when they click a certain item, I want the line_one_unit_price to get populated with the price of the item selected in the line_one option!所以基本上用户使用Inventory模型添加产品,然后在Invoice模型中,他们将有一个line_one的下拉菜单,当他们单击某个项目时,我希望line_one_unit_price填充该项目的价格在line_one选项中选择!
InvoiceForm:发票形式:
class InvoiceForm(forms.ModelForm):
line_one_unit_price = forms.CharField(widget=forms.Select, label="Unit Price(₹)")
class Meta:
model = Invoice
fields = ['line_one',#...]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['line_one_unit_price'].widget.choices = [(i.amount, i.amount) for i in Inventory.objects.all()]
By using the above logic, I can list the amount of all products present in the Inventory but I want it to only display the amount of the product that the user has selected in line_one .通过使用上述逻辑,我可以列出 Inventory 中存在的所有产品的数量,但我希望它只显示用户在line_one中选择的产品数量。
In the image, the amount of the first product is automatically added but instead, I want it to be "-----" by default.在图像中,第一个产品的数量是自动添加的,但我希望它默认为“-----”。 How can I implement that?
我该如何实施? Thank you.
谢谢你。
There's different ways you can do this, but essentially there's no built-in simple way.有不同的方法可以做到这一点,但基本上没有内置的简单方法。 You need to employ JavaScript on the front-end to detect when that "Line 1" field changes and then fill in the "Unit Price" accordingly.
您需要在前端使用 JavaScript 来检测“第 1 行”字段何时发生变化,然后相应地填写“单价”。
If you don't have too many "Line 1" items, one way of doing it is creating your own widget to embed the price in each <option>
of the drop-down with a data-
attribute.如果您没有太多“第 1 行”项目,一种方法是创建自己的小部件,以将价格嵌入到带有
data-
属性的下拉列表的每个<option>
中。 Then you include some JavaScript that detects when that drop-down has changed and updates the read-only "Unit Price" field.然后,您包含一些 JavaScript,用于检测该下拉菜单何时更改并更新只读“单价”字段。 On the back-end, you should also make sure you don't use that form field and look-up the price based on the "Line 1" field.
在后端,您还应确保不使用该表单字段并根据“第 1 行”字段查找价格。
Another option is to make an AJAX call to an endpoint to fetch the price.另一种选择是对端点进行 AJAX 调用以获取价格。
EDIT: If there's multiple drop-downs with the same list of items, then having a global JS variable with a price look-up might work best (if not too large).编辑:如果有多个下拉菜单包含相同的项目列表,那么使用带有价格查询的全局 JS 变量可能效果最好(如果不是太大的话)。 Generate a look-up
dict
of product_number
to amount
(price), pass that to the template to output as JSON in a global variable, and then use that in your JavaScript to populate the "Unit Price".生成
product_number
到amount
(价格)的查找dict
,将其传递给模板以在全局变量中作为 JSON 输出,然后在 JavaScript 中使用它来填充“单价”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.