【问题标题】:Need to store Cart Item in Django需要在 Django 中存储购物车项目
【发布时间】:2023-02-26 15:34:45
【问题描述】:

我必须在 Django REST 框架中存储 CartItem: {"title": "bread", "price": "100.0"}。 应用名称:BackendDjangoApp 但我收到错误:

“返回 Database.Cursor.execute(self, query, params)

django.db.utils.OperationalError:没有这样的表:BackendDjangoApp_cartitem [26/Feb/2023 11:53:06] “POST /api/cartItems/ HTTP/1.1” 500 192021”

意见.py:

class CartItemView(generics.ListCreateAPIView):
    queryset = CartItem.objects.all()
    serializer_class = CartItemSerializer

序列化程序.py:

class CartItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = CartItem
        fields = '__all__'

模型.py:

from django.db import models
from django.contrib.auth.models import User

class CartItem(models.Model):
    title = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=10, decimal_places=2)

我正在发出 POST 请求:http://127.0.0.1:8000/api/cartItems/

POST 正文:{“标题”:“面包”,“价格”:“100.0”}

我没有维护任何用户会话。

【问题讨论】:

  • 您是否迁移了模型?进行迁移:python manage.py makemigrations 迁移:python manage.py migrate

标签: django


【解决方案1】:

只需运行 python manage.py makemigrations 和 python manage.py migrate 就可以了。

【讨论】:

    猜你喜欢
    • 2016-06-06
    • 2020-01-04
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多