【问题标题】:I have an issue with the changeStock method of a Product class in views.py in Django我对 Django 中 views.py 中 Product 类的 changeStock 方法有疑问
【发布时间】:2020-08-23 12:48:15
【问题描述】:

我在 Django 中做一个产品类是为了好玩,我遇到了一个关于更改产品(型号名称)的问题,该变量用于检查该产品的库存量。

目标是使用 changeStock 方法将产品的 qtyOnHand 更改指定数量,并重定向到将存储更新的 qtyOnHand 的主页。如果取出库存,金额可以为负数,如果收到库存,则为 +ve。我在输入 URL 时遇到的错误 http://127.0.0.1:8000/products/1/changeStock/2 是 + 不支持的操作数类型:int 和 str,这是

这是我在 views.py 中的 changeStock 方法的代码。那里的 cmets 可以指导我解决问题的方法。

# Change the stock of a specific product by the specified amount.  A -ve number means that you are taking out product, 
# while a +ve one means you are receiving that product.
def changeStock(request, pk, amount):
    # We need the id of a specific product, plus we have to check if it's not found.
    product = get_object_or_404(Product, pk=pk)
    
    # Then we have to update its qtyOnHand.
    product.qtyOnHand = product.qtyOnHand + amount -> error occurs at this line
    
    # Then we have to save the changes to the database.
    product.save()
    
    # Then we have to redirect to the home page.
    return render(request, 'home.html', {'product': product})

这是我的 urls.py,用于存储我的 URL。 (?P-?\d+) 部分用于包含正数和负数。

from django.conf.urls import url
from django.contrib import admin
from inventories import views

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^products/(?P<pk>\d+)/$', views.product_topics, name='product_topics'),
    url(r'^products/(?P<pk>\d+)/new/$', views.new_product, name='new_product'),
    url(r'^products/(?P<pk>\d+)/changeStock/(?P<amount>-?\d+)$', views.changeStock, name='home'),
    url(r'^admin/', admin.site.urls),
]

在我的 home.py 中,显示所有产品

{% extends 'base.html' %}

{% block breadcrumb %}
  <li class="breadcrumb-item active">Products</li>
{% endblock %}

{% block content %}
  <table class="table">
    <thead class="thead-inverse">
      <tr>
        <th>Products</th>
        <th>Price</th>
        <th>Quantity on Hand</th>
      </tr>
    </thead>
    <tbody>
      {% for product in products %}
        <tr>
          <td>
            <a href="{% url 'product_topics' product.pk %}">{{ product.name }}</a>
            <small class="text-muted d-block">{{ product.description }}</small>
          </td>
          <td class="align-middle">
            {{ product.price }}
          </td>
          <td class="align-middle">{{ product.qtyOnHand }} </td>
        </tr>
      {% endfor %}
    </tbody>
  </table>

{% endblock %}

这是我的 Product 类的 models.py

from django.db import models

# Create your models here.
class Product(models.Model):
    name = models.CharField(max_length=30)
    description = models.TextField(max_length=4000)
    price = models.DecimalField(max_digits=19,decimal_places=2)
    qtyOnHand = models.IntegerField()

    def __str__(self):
        return self.name

谁能告诉我这个错误是什么意思,我该如何解决?

【问题讨论】:

  • product.qtyOnHand 是一个整数。
  • 我认为问题是product.qtyOnHand + amount 应该是product.qtyOnHand + int(amount)。默认情况下,传递给 Django 视图的所有参数都是字符串
  • 感谢 mattyx17 帮助我解决这个问题。我将其更改为 int(amount) 并解决了所有问题。原来这是您将 int 添加到字符串时的问题。

标签: python django


【解决方案1】:

您传递的参数在视图中被视为string。因此将其转换为intproduct.qtyOnHand 始终是模型中定义的 int。所以不要将 string 类型添加到 int。请将 product.qtyOnHand = product.qtyOnHand + amount 更改为

product.qtyOnHand = product.qtyOnHand + int(amount)

我建议这样。

【讨论】:

  • 所以这意味着错误是由类型转换问题引起的。
猜你喜欢
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 2020-03-25
  • 2021-09-28
  • 1970-01-01
  • 2017-01-13
相关资源
最近更新 更多