如何使用自定义表单创建基于类的视图以对对象执行操作

时间:2016-12-27 19:27:38

标签: python django python-3.x

对于盘点界面,我希望用户为库存中的新金额项插入小数。具有此十进制数的交易将通过添加差异将库存中的项目设置为实际值。

要做哟我添加了一个视图,只提供一个用于插入小数

的字段

views.py:

class StorageItemStockTakingView(FormView):
    form_class = StockTakingForm
    success_url = reverse_lazy('storage_item_list')
    template_name = 'pmgmt/storageitem/stocktaking.html'

    def form_valid(self, form):
        si = StorageItem.objects.get(pk=self.kwargs["pk"])
        si.stock_report(
            Decimal(self.request.POST["amount"]), self.request.user)
        return super(StorageItemStockTakingView, self).form_valid(form)

基于forms.py

中的自定义表单
class StockTakingForm(forms.Form):
    amount = forms.DecimalField(
        _("Parts now inside storage"),
        max_digits=10,
        decimal_places=4,
        help_text=_("The amount of currently inside storage place."))

使用过的模板很简单:

{% extends "base.html" %}

{% load i18n %}
{% load semanticui %}

{% block content %}
<h1>{{ object }}</h1>
<p>{% trans "It will create an Transaction adjusting the amount for items on stock to reported one.." %}</p>
<form method="post" class="ui form segment">
  {% csrf_token %}
  {{ form|semanticui }}
  <input class="ui button blue" type="submit" value="Submit">
</form>
{% endblock %}

这确实适用于Python2。但是当我在Python3上使用它时,我得到了一个错误

2016-12-27 19:17:57,206 django.request 24760 Thread-2 ERROR Internal Server Error: /pmgmt/storageitem/1/stocktaking/
Traceback (most recent call last):
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/edit.py", line 221, in post
    if form.is_valid():
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 161, in is_valid
    return self.is_bound and not self.errors
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 153, in errors
    self.full_clean()
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 362, in full_clean
    self._clean_fields()
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 380, in _clean_fields
    value = field.clean(value)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/fields.py", line 168, in clean
    self.run_validators(value)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/fields.py", line 151, in run_validators
    v(value)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/validators.py", line 310, in __call__
    if self.compare(cleaned, self.limit_value):
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/validators.py", line 324, in <lambda>
    compare = lambda self, a, b: a > b
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/utils/functional.py", line 148, in __lt__
    return self.__cast() < other
TypeError: unorderable types: str() < decimal.Decimal()
2016-12-27 19:17:57,206 django.request 24760 Thread-2 ERROR Internal Server Error: /pmgmt/storageitem/1/stocktaking/
Traceback (most recent call last):
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/edit.py", line 221, in post
    if form.is_valid():
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 161, in is_valid
    return self.is_bound and not self.errors
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 153, in errors
    self.full_clean()
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 362, in full_clean
    self._clean_fields()
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 380, in _clean_fields
    value = field.clean(value)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/fields.py", line 168, in clean
    self.run_validators(value)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/fields.py", line 151, in run_validators
    v(value)
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/validators.py", line 310, in __call__
    if self.compare(cleaned, self.limit_value):
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/validators.py", line 324, in <lambda>
    compare = lambda self, a, b: a > b
  File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/utils/functional.py", line 148, in __lt__
    return self.__cast() < other
TypeError: unorderable types: str() < decimal.Decimal()

所以我想知道在基于类的视图上下文中使用最近的Django版本做这样的事情的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

forms.DecimalField的第一个位置参数不是标签,而是最大值 - 这就是错误抱怨将字符串(该值)与int(字段的实际值)进行比较的原因。

通常,您不应将位置参数与表单字段类一起使用 - 始终使用kwargs:

amount = forms.DecimalField(
    label=_("Parts now inside storage"),
    ...)