django中如何赋值

发布于 2024-12-19 02:25:23 字数 3670 浏览 1 评论 0原文

我在减去两个值时遇到问题。我只想成为这样 a = b - c

这是我在 handlers.py 中的代码

 def create(self, request): 
        if not self.has_model():
            return rc.NOT_IMPLEMENTED

        attrs = self.flatten_dict(request.POST)

        if attrs.has_key('data'):
            ext_posted_data = SimplerXMLGenerator(request.POST.get('data'))
            attrs = self.flatten_dict(ext_posted_data)

        prod = Product.objects.get(id=attrs['id'])
        prod_quantity = prod.quantity

        quantity_order = attrs['quantity']

        sumOfQuantity = Booking.objects.filter(date_select=attrs['date_select']).aggregate(Sum('quantity')

        prodAvailable = prod_quantity - sumOfQuantity

        if prodAvailable = 0:
            #select another date
            return rc.NOT_HERE
        if prodAvailable <= quantity_order:
            return prodAvailable
        else :
            total = float(quantity_order) * prod.price
            inst = self.model(
                date_select = attrs['date_select'],
                product_name = prod.name,
                quantity = attrs['quantity'],
                price = prod.price,
                totalcost = total,
                first_name = attrs['first_name'],
                last_name = attrs['last_name'],
                contact = attrs['contact'],
                product = prod
                           )
            inst.save()
            return inst

问题出在 prodAvailable = prod_quantity - sumOfQuantity 中 我的问题是我怎样才能正确声明它?

预先感谢:p

这是我的回溯...

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  89.                     response = middleware_method(request)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in process_request
  67.             if (not _is_valid_path(request.path_info, urlconf) and
File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in _is_valid_path
  154.         urlresolvers.resolve(path, urlconf)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  342.     return get_resolver(urlconf).resolve(path)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  252.                     sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  250.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_url_patterns
  279.         patterns = getattr(self.urlconf_module, &quot;urlpatterns&quot;, self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_urlconf_module
  274.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/agileone/workspace/bookproj/../bookproj/api/urls.py" in &lt;module&gt;
  3. from api.handlers import *

Exception Type: SyntaxError at /api/bookings
Exception Value: invalid syntax (handlers.py, line 94)

此外,当我尝试在 python shell 中执行此操作时...它是这样的:

>>> sumOfQuantity = Booking.objects.filter(date_select='2011-11-29').aggregate(Sum('quantity'))
>>> print sumOfQuantity
{'quantity__sum': 2}
>>> prod_quantity = 1
>>> prodAvailable = prod_quantity - sumOfQuantity
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'int' and 'dict'

I have a problem in subtracting a two values. i just want to be like this
a = b - c

here is my code in my handlers.py

 def create(self, request): 
        if not self.has_model():
            return rc.NOT_IMPLEMENTED

        attrs = self.flatten_dict(request.POST)

        if attrs.has_key('data'):
            ext_posted_data = SimplerXMLGenerator(request.POST.get('data'))
            attrs = self.flatten_dict(ext_posted_data)

        prod = Product.objects.get(id=attrs['id'])
        prod_quantity = prod.quantity

        quantity_order = attrs['quantity']

        sumOfQuantity = Booking.objects.filter(date_select=attrs['date_select']).aggregate(Sum('quantity')

        prodAvailable = prod_quantity - sumOfQuantity

        if prodAvailable = 0:
            #select another date
            return rc.NOT_HERE
        if prodAvailable <= quantity_order:
            return prodAvailable
        else :
            total = float(quantity_order) * prod.price
            inst = self.model(
                date_select = attrs['date_select'],
                product_name = prod.name,
                quantity = attrs['quantity'],
                price = prod.price,
                totalcost = total,
                first_name = attrs['first_name'],
                last_name = attrs['last_name'],
                contact = attrs['contact'],
                product = prod
                           )
            inst.save()
            return inst

The problem is in prodAvailable = prod_quantity - sumOfQuantity
my question is how can i declare it correctly?

thanks in advance :p

here is my trace back ...

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  89.                     response = middleware_method(request)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in process_request
  67.             if (not _is_valid_path(request.path_info, urlconf) and
File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in _is_valid_path
  154.         urlresolvers.resolve(path, urlconf)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  342.     return get_resolver(urlconf).resolve(path)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  252.                     sub_match = pattern.resolve(new_path)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  250.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_url_patterns
  279.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_urlconf_module
  274.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/agileone/workspace/bookproj/../bookproj/api/urls.py" in <module>
  3. from api.handlers import *

Exception Type: SyntaxError at /api/bookings
Exception Value: invalid syntax (handlers.py, line 94)

furthermore, when i trying to do it in my python shell... it goes like this:

>>> sumOfQuantity = Booking.objects.filter(date_select='2011-11-29').aggregate(Sum('quantity'))
>>> print sumOfQuantity
{'quantity__sum': 2}
>>> prod_quantity = 1
>>> prodAvailable = prod_quantity - sumOfQuantity
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'int' and 'dict'

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

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

发布评论

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

评论(1

月朦胧 2024-12-26 02:25:23

如果您确实希望它看起来像 a = b - c 那么您必须将您的分配更改为 sumOfQuantity

sumOfQuantity = Booking.objects.filter(date_select='2011-11-29').aggregate(Sum('quantity'))['quantity__sum']

If you really want it to look like a = b - c then you'll have to change your assignment to sumOfQuantity:

sumOfQuantity = Booking.objects.filter(date_select='2011-11-29').aggregate(Sum('quantity'))['quantity__sum']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文