【问题标题】:Grabbing POST variable in form clean() method在表单 clean() 方法中获取 POST 变量
【发布时间】:2010-10-31 15:28:31
【问题描述】:

是否有一种直接的方法可以访问 POST 变量以在管理表单字段的 clean 方法中完成一些自定义验证查找?

def clean_order_price(self):
  if not self.cleaned_data['order_price']:
    try:
      existing_price = ProductCostPrice.objects.get(supplier=supplier, product_id=self.cleaned_data['product'], is_latest=True)
    except ProductCostPrice.DoesNotExist:
      existing_price = None
    if not existing_price:
      raise forms.ValidationError('No match found, please enter new price')
    else:
      return existing_price.cost_price_gross
  else:
      return self.cleaned_data

我需要获取的是“供应商”发布变量,它不在此表单的已清理数据中,因为供应商字段是父表单的一部分。我能看到获取它的唯一方法是访问 request.POST 但在那里没有太大成功。

谢谢

【问题讨论】:

    标签: django django-admin django-forms validation


    【解决方案1】:

    POST 数据包含在self.data 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      相关资源
      最近更新 更多