当从表单中另一个字段中的下拉菜单中选择一个值时,如何自动填充Django中的字段?

发布于 2025-02-13 04:22:30 字数 2163 浏览 0 评论 0 原文

我有两个位于两个不同应用程序中的模型。 发票库存

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 被引用 integtory.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

因此,用户基本上使用了产品, 库存型号,然后在发票模型中,他们将提供 line_one 的下拉菜单,当他们单击时某个项目,我希望 line_one_unit_price 能够用 line_one 选项中选择的项目价格填充!

InvoIteform:

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()]

通过使用上述逻辑,我可以列出库存中所有产品的数量,但我希望它仅显示用户在 line_one line_one < /strong>。

截屏:

在图像中,第一个产品的数量自动添加,但我希望它是 “ - - -“ 默认情况下。 我该如何实施?谢谢。

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

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!

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.

Screenshot:
screenshot

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2025-02-20 04:22:30

您可以做到不同的方法,但是从本质上讲,没有内置的简单方法。您需要在前端使用JavaScript来检测何时“ 1行”字段更改,然后相应地填写“单位价格”。

如果您没有太多的“第1行”项目,一种方法是创建自己的小部件,以将下拉纸的每个&gt; 嵌入使用 >数据 - 属性。然后,您包括一些JavaScript,这些JavaScript检测到该下拉何时更改并更新仅阅读的“单位价格”字段。在后端,您还应确保不使用该表单字段并根据“第1行”字段查找价格。

另一个选择是将Ajax调用到端点以获取价格。

编辑:如果有多个下拉列表具有相同的项目列表,则具有价格查找的全局JS变量可能最有效(如果不是太大)。生成 product> product_number 的查找 dict (价格),将其传递给模板中的json在全局变量中作为JSON,并且然后在您的JavaScript中使用它来填充“单位价格”。

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.

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. Then you include some JavaScript that detects when that drop-down has changed and updates the read-only "Unit Price" field. 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.

Another option is to make an AJAX call to an endpoint to fetch the price.

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). 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文