I am trying to import categories but lines that have a parent is causing an issue
I have an empty product category table and I am trying to import categories from a csv file and I can import other categories for other apps but not for products so I had the csv file checked and I'm told it's fine. I get the following errors and they all start at the line that has a parent.
Line number: 29 - ProductCategory matching query does not exist.
Line number: 30 - ProductCategory matching query does not exist.
Line number: 31 - ProductCategory matching query does not exist.
Line number: 32 - ProductCategory matching query does not exist.
Line number: 33 - ProductCategory matching query does not exist.
If it's a new import why is it looking for an existing category? If I remove all the lines that has a parent then I can import and after importing and putting the lines back that has a parent I get the same error again, which now does not make sense.
Here is my admin.py file:
from django.contrib import admin
from .models import *
from import_export import resources
from import_export.admin import ImportExportModelAdmin
class CategoryResource(resources.ModelResource):
class Meta:
model = ProductCategory
fields = ("id", "name", "description", "parent")
class ProductCategoryAdmin(ImportExportModelAdmin):
list_display = ("name", "id", "parent")
search_fields = ["name", "id", "parent"]
resource_class = CategoryResource
class BrandResource(resources.ModelResource):
class Meta:
model = Brand
fields = ("name", "category")
class BrandAdmin(ImportExportModelAdmin):
list_display = ("name", "category")
search_fields = ["name", "category"]
resource_class = [BrandResource]
class ProductResource(resources.ModelResource):
class Meta:
model = Product
fields = (
"id",
"title",
"description",
"price",
"color",
"height",
"width",
"length",
"weight",
"material",
"digital",
"condition",
"cordless",
"hand_made",
"power_usage",
"device_storage",
"ram",
"screen_size",
"cpu",
"gpu",
"size",
"capacity",
"age",
"type",
"gender",
"sports",
"business",
"brand",
"user",
"sale",
)
class ProductAdmin(ImportExportModelAdmin):
list_display = ("title", "category", "price", "business")
search_fields = ["title", "category"]
resource_classes = [ProductResource]
class ProductImageAdmin(admin.ModelAdmin):
list_display = ("product", "image")
search_fields = ["gallery"]
class ProductVideoAdmin(admin.ModelAdmin):
list_display = ("product", "video")
search_fields = ["product"]
class CartItemAdmin(admin.ModelAdmin):
list_display = ('cart', 'product', 'quantity', 'total_price')
search_fields = ['cart__user__username', 'product__title']
list_filter = ['cart__user', 'product']
def total_price(self, obj):
return obj.total_price()
total_price.short_description = 'Total Price'
class CartItemInline(admin.TabularInline):
model = CartItem
extra = 0
readonly_fields = ['total_price']
fields = ['product', 'quantity', 'total_price']
def total_price(self, obj):
if obj.product and obj.quantity:
return obj.product.price * obj.quantity
return 0
total_price.short_description = 'Total Price'
def get_extra(self, request, obj=None, **kwargs):
return 1
def has_add_permission(self, request, obj):
return obj is not None
class UserCartAdmin(admin.ModelAdmin):
list_display = ("user", "display_items")
search_fields = ["user"]
inlines = [CartItemInline]
def display_items(self, obj):
items = obj.items.all()
return ", ".join([f"{item.quantity} x {item.product.title}" for item in items])
display_items.short_description = 'Items in Cart'
admin.site.register(Product, ProductAdmin)
admin.site.register(ProductCategory, ProductCategoryAdmin)
admin.site.register(Brand, BrandAdmin)
admin.site.register(ProductImage, ProductImageAdmin)
admin.site.register(ProductVideo, ProductVideoAdmin)
admin.site.register(CartItem, CartItemAdmin)
admin.site.register(Cart, UserCartAdmin)
How do I fix this please?