Django表单:根据一个字段中的用户输入更新模型值

2024-06-25 22:50:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个表单,让用户从模板中输入购买信息。需要做几件事:

  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)

我不知道如何让用户在表单中输入值,并自动将Quantity添加到StockOnOrder,以及基于Supplier自动识别SupplierID。在这一点上,我真的不知道从哪里开始。至少,有可能实现我想做的事情吗


Tags: 用户truedefault表单modelsrequiredformslength
2条回答

这只是一个大概的想法。你可以试试这样的

首先从有效表单中获取用户输入的数量和供应商值

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

相关问题 更多 >