簡體   English   中英

Django 表單:根據用戶在一個字段中的輸入更新模型值

[英]Django form: Update model values based on user input in one field

我正在編寫一個表單,讓用戶從模板輸入購買。 有幾件事需要發生:

  1. 購買將填充replenishment表中的一行
  2. replenishment表的某些字段會根據用戶輸入的內容進行更新

這是我的模型的樣子:

class replenishment(models.Model):
    Id = models.CharField(max_length=100, primary_key=True, verbose_name= 'references')
    Name = models.CharField(max_length=200)
    Quantity = models.FloatField(default=0)
    NetAmount = models.FloatField(default=0)
    SupplierID = models.CharField(max_length=200)
    Supplier = models.CharField(max_length=200)
    SellPrice = models.FloatField(default=0)
    StockOnOrder = models.FloatField(default=0)
    StockOnHand = models.FloatField(default=0)
    
    def __str__(self):
        return self.reference

和形式:

class ProcurementOperationRecord(forms.Form)
    Id = forms.CharField(required=True)
    Quantity = forms.FloatField(required=True)
    NetAmount = forms.FloatField(required=True)
    Supplier = forms.CharField(required=True)
    SellPrice = forms.FloatField(required=True)

我不知道如何讓用戶在輸入表單中的值,並自動添加QuantityStockOnOrder以及自動識別SupplierID基於Supplier 在這一點上,我真的不知道從哪里開始。 至少,有可能實現我嘗試做的事情嗎?

這只是一個普遍的想法。 你可以嘗試這樣的事情。

首先從有效表單中像這樣獲取用戶輸入的數量和供應商值

quantity = form.cleaned_data.get('quantity')
supplier = form.cleaned_data.get('supplier')

然后你可以更新你的replenishment模型

replenishment.objects.filter(Supplier=supplier).update(StockOnOrder=quantity)

首先,我改變了一些事情,並添加了一些評論,說明我做了什么以及為什么這樣做。

# models/classes in python are singular AND camel cased (99.9%)
class Supplier(models.Model):
    ...

# models/classes in python are singular AND camel cased (99.9%)
class Replenishment(models.Model):
    # attributes are normally lower case and snake cased (99.9%)
    
    # try not to do this, a CharField??, unless you're using a guid? if so use UUIDField()
    # https://docs.djangoproject.com/en/3.1/ref/models/fields/#uuidfield
    id = models.CharField(db_column='Id', max_length=100, primary_key=True, verbose_name='references') 
    name = models.CharField(db_column='Name', max_length=200)
    quantity = models.FloatField(db_column='Quantity', default=0)
    net_amount = models.FloatField(db_column='NetAmount', default=0)
    # deleted your field "Supplier" -- with this change you can join to the other table and get what you need without having to duplicate anything
    supplier = models.ForeignKey(Supplier, db_column='SupplierID')
    sell_price = models.DecimalField(db_column='SellPrice', default=0, max_digits=6, decimal_places=2)  # You're asking for trouble if you keep this as FloatField
    stock_on_order = models.IntegerField(db_column='StockOnOrder', default=0)  # how can you have ordered a .5 for your stock? changed to IntegerField
    stock_on_hand = models.IntegerField(db_column='StockOnHand', default=0)  # how can you have a .5 of your stock? changed to IntegerField

    class Meta:
        db_table = 'replenishment'  # try not to do this either.. let django come up with the name.. unless you're using an existing database/table?

    ...

# models/classes in python are singular AND camel cased (99.9%)
# django has a standard that they normally postfix forms with "Form" at the end of the class (no matter if it's a ModelForm or regular Form)
class ProcurementOperationRecordForm(forms.ModelForm)
    class Meta:
        model = Replenishment
        fields = ('id', 'quantity', 'net_amount', 'supplier', 'sell_price')
        # I would remove the "id", the client shouldn't care or know about it..

現在創建和更新。 (這將住在一個視圖中)

# creating?
form = ProcurementOperationRecordForm(data=request.POST)
if form.is_valid():
    form.save()
    return redirect(..) or render(..)

# updating?
replenishment = Replenishment.objects.get(id='...something')
form = ProcurementOperationRecordForm(data=request.POST, instance=replenishment)
if form.is_valid():
    form.save()
    return redirect(..) or render(..)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM