gpt4 book ai didi

python - django 导入导出 : import not working

转载 作者:行者123 更新时间:2023-12-04 04:18:24 24 4
gpt4 key购买 nike

我试图用 .csv 文件和 django-import-export 模块填充我的数据库,但我一直在运行这个错误

行号:1 - 调用 Python 对象时超出最大递归深度 43, Sicilian, L, 0, 1,7, 45.7
每当我尝试通过管理 UI 填充我的 Pizza 模型时。

这是我的 CSV

csv file

admin.py

from django.contrib.admin import ModelAdmin

from import_export.admin import ImportExportModelAdmin

from .models import Topping, Pizza, Sub, Pasta, Salad, Dinner


class PizzaAdmin(ImportExportModelAdmin):
def save_related(self, request, form, formsets, change):
super(PizzaAdmin, self).save_related(request, form, formsets, change)
form.instance.toppings.add(Topping.objects.get(name='Cheese'))

@admin.register(Sub)
class SubAdmin(ImportExportModelAdmin):
pass

# Register your models here.
admin.site.register(Topping)
admin.site.register(Pizza, PizzaAdmin)
# admin.site.register(Sub)
admin.site.register(Pasta)
admin.site.register(Salad)
admin.site.register(Dinner)

模型.py

from django.contrib import admin

from django.db import models
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.db.models.signals import post_save

from django.core.validators import MinValueValidator, MaxValueValidator
from django.contrib.auth import get_user_model

# Create your models here.


class Topping(models.Model):
name = models.CharField(max_length=64)

def __str__(self):
return(f"{self.name}")


class Pizza(models.Model):
PIZZA_SIZES = (
('S', 'Small'),
('L', 'Large'),
)

pizza_type = models.CharField(max_length=64)
pizza_size = models.CharField(max_length=1, choices=PIZZA_SIZES)
qty_toppings = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(3)], default=0)
toppings = models.ManyToManyField(Topping, related_name='pizzas', blank=True)
price = models.FloatField(help_text="Price in $")


def __str__(self):
return f"Size: {self.get_pizza_size_display()}, Type: {self.pizza_type}, Number of Toppings: {self.qty_toppings}, Price: {self.price}, Toppings: {self.toppings.in_bulk()}"

def save(self, *args, **kwargs):
# if 'toppings' not in kwargs:
# kwargs.setdefault('force_insert', True)
# kwargs.setdefault('force_update', True)
super(Pizza, self).save(*args, **kwargs)
self.toppings.add(Topping.objects.get(name='Cheese'))
# kwargs.setdefault('toppings', Topping.objects.get(name='Cheese'))


class Sub(models.Model):
SUBS_SIZES = (
('S', 'Small'),
('L', 'Large'),
)

subs_size = models.CharField(max_length=1, choices=SUBS_SIZES)
name = models.CharField(max_length=64)
price = models.IntegerField(help_text="Price in $")

def __str__(self):
return f"{self.name}, {self.get_subs_size_display()} : {self.price}"

class Pasta(models.Model):
name = models.CharField(max_length=64)
price = models.IntegerField(help_text="Price in $")

def __str__(self):
return f"{self.name} : {self.price}"


class Salad(models.Model):
name = models.CharField(max_length=64)
price = models.IntegerField(help_text="Price in $")

def __str__(self):
return f"{self.name} : {self.price}"


class Dinner(models.Model):
DINNER_SIZES = (
('S', 'Small'),
('L', 'Large'),
)

dinner_size = models.CharField(max_length=1, choices=DINNER_SIZES)

name = models.CharField(max_length=64)
price = models.IntegerField(help_text="Price in $")

def __str__(self):
return f"{self.name}, {self.get_dinner_size_display()} : {self.price}"


class Euser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)

def __str__(self):
return(f"{self.id}")


class ShoppingCart(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
pizzas = models.ManyToManyField(Pizza)
subs = models.ManyToManyField(Sub)
pastas = models.ManyToManyField(Pasta)
dinners = models.ManyToManyField(Dinner)

number_of_articles = models.IntegerField(null=True)
price = models.DecimalField(max_digits=5, decimal_places=2, null=True)

def __str__(self):
return(f"{self.user}'s cart")

@receiver(post_save, sender=get_user_model())
def create_user_cart(sender, instance, created, **kwargs):
if created:
ShoppingCart.objects.create(user=instance)




# @receiver(post_save, sender=User)
# def create_user_cart(sender, instance, created, **kwargs):
# if created:
# ShoppingCart.objects.create(user=instance)

# @receiver(post_save, sender=User)
# def save_user_cart(sender, instance, **kwargs):
# instance.shoppingcart.save()

仅供引用:使用 pandas 将相同的 .csv 转换为 .json,我在尝试读取文件时遇到 KeyError:pizza_sicilian.json

任何帮助将不胜感激

最佳答案

我遇到了同样的问题我宁愿建议你

in admin .py
@admin.register(Sub)
class SubAdmin(ImportExportModelAdmin):
list_display=["pizza","other objects in model"]
pass

list_display 是一个默认函数,项目应该在列表中......这解决了我的问题,我认为它会帮助很多观众!谢谢你!

关于python - django 导入导出 : import not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60000191/

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