gpt4 book ai didi

python - Django:manage.py loaddata 返回 'Deserialization Error'

转载 作者:行者123 更新时间:2023-12-01 00:25:29 24 4
gpt4 key购买 nike

因此,当我尝试从 cmd 将数据从 .json 文件加载到模型中时,出现以下错误: enter image description here

这似乎是“category_id”的问题,但我不确定如何解决这个问题。

这是我的代码:

shop/models.py

from django.db import models
from django.urls import reverse


# Create your models here.
class Category(models.Model):
name = models.TextField()
products = models.ManyToManyField('Product')

def get_products(self):
return Product.objects.filter(category=self)

def __str__(self):
return self.name

def get_absolute_url(self):
return reverse('product_list_by_category',args=[self.id])


class Product(models.Model):
name = models.TextField()
description = models.TextField()
stock = models.IntegerField()
price = models.DecimalField(max_digits=10,decimal_places=2)
image = models.ImageField(upload_to='images/', blank=True)

def __str__(self):
return self.name

商店/views.py

from django.shortcuts import render, get_object_or_404, redirect
from .models import Category, Product
from django.db.models import Count
from django.contrib.auth.models import Group, User
from django.core.paginator import Paginator, EmptyPage, InvalidPage

def product_list(request, category_id=None):
category = None
products = Product.objects.all()
ccat = Category.objects.annotate(num_products=Count('products'))
if category_id:
category = get_object_or_404(Category, id=category_id)
products = products.filter(category=category)
paginator = Paginator(products, 3)
try:
page = int(request.GET.get('page', 1))
except:
page = 1
try:
products = paginator.page(page)
except(EmptyPage, InvalidPage):
products = paginator.page(paginator.num_pages)

return render(request, 'products.html',
{'products': products,
'countcat':ccat})

这是我希望加载到数据库中的两个 .json 文件(值得注意的是 createcategories.json 工作没有问题)

创建类别.json

[
{
"model": "shop.category",
"pk": 1,
"fields": {
"name": "Europe"
}
},
{
"model": "shop.category",
"pk": 2,
"fields": {
"name": "North America"
}
},
{
"model": "shop.category",
"pk": 3,
"fields": {
"name": "South America"
}
},
{
"model": "shop.category",
"pk": 4,
"fields": {
"name": "Asia"
}
},
{
"model": "shop.category",
"pk": 5,
"fields": {
"name": "Africa"
}
},
{
"model": "shop.category",
"pk": 6,
"fields": {
"name": "Middle East"
}
}
]

I thought that the category_id would get added automatically as a foreign key bu it has not.

Many thanks in advance!


创建产品.json

[
{
"model": "shop.product",
"pk": 1,
"fields": {
"category_id":1,
"name": "Europe Title 1",
"description": "Europe Description 1",
"stock": 3,
"price": 100.00
}
},
{
"model": "shop.product",
"pk": 2,
"fields": {
"category_id":2,
"name": "North America Title 1",
"description": "North America Description 1",
"stock": 4,
"price": 7.00
}
},
{
"model": "shop.product",
"pk": 3,
"fields": {
"category_id":3,
"name": "South America Title 1",
"description": "South America Desc 1",
"stock": 100,
"price": 2.00
}
},
{
"model": "shop.product",
"pk": 4,
"fields": {
"category_id":4,
"name": "Asia Title 1",
"description": "Asia Desc 1",
"stock": 50,
"price": 1.00
}
},
{
"model": "shop.product",
"pk": 5,
"fields": {
"category_id":5,
"name": "Africa Title 1",
"description": "Africa Desc 1",
"stock": 9,
"price": 99.00
}
},
{
"model": "shop.product",
"pk": 6,
"fields": {
"category_id":4,
"name": "Asia Title 2",
"description": "Asia Desc 2",
"stock": 1,
"price": 50.00
}
},
{
"model": "shop.product",
"pk": 7,
"fields": {
"category_id":1,
"name": "Europe Title 2",
"description": "Europe Desc 2",
"stock": 2,
"price": 300.00
}
},
{
"model": "shop.product",
"pk": 8,
"fields": {
"category_id":4,
"name": "Asia Title 3",
"description": "Asia Desc 3",
"stock": 2,
"price": 75.00
}
},
{
"model": "shop.product",
"pk": 9,
"fields": {
"category_id":6,
"name": "Middle East Title 1",
"description": "Middle East Desc 1",
"stock": 1,
"price": 50.00
}
},
{
"model": "shop.product",
"pk": 10,
"fields": {
"category_id":6,
"name": "Middle East Title 2",
"description": "Middle East Desc 1",
"stock": 2,
"price": 15.00
}
}
]

最佳答案

而不是 category_idcreateproducts.json ,你应该有productscreatecategories.json .

我的猜测是,当创建转储时,有一个 ForeignKey关系来自ProductCategory ,但现在有一个 ManyToMany关系来自CategoryProduct .

如果您可以在没有类别分配的情况下生存,则可以删除 category_id条目。否则你必须重写这两个文件。

关于python - Django:manage.py loaddata 返回 'Deserialization Error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58611557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com