I have the following model for a website category
class Category(models.Model):
name = models.CharField(max_length=50)
description = models.TextField(max_length=500, blank=True, null=True)
parent = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True ,related_name='sub_categories')
created_at = models.DateTimeField(auto_now_add=True, verbose_name="created at")
updated_at = models.DateTimeField(auto_now=True, verbose_name="updated at")
class Meta:
verbose_name = "category"
verbose_name_plural = "categories"
db_table = "website_categories"
unique_together = ('name', 'parent',)
def __str__(self):
return self.name
I have installed django-import-export model and have the following set up for the admin
from .models import *
from import_export import resources
from import_export.admin import ImportExportModelAdmin
from django.contrib.auth import get_user_model
User = get_user_model()
class WebsiteAdmin(admin.ModelAdmin):
list_display = ("url", "category", "user")
search_fields = ["url", "category", "user"]
class ServiceCategoryAdmin(admin.ModelAdmin):
list_display = ("name",)
search_fields = ["name"]
class ProductCategoryAdmin(admin.ModelAdmin):
list_display = ("name",)
search_fields = ["name"]
class CategoryResource(resources.ModelResource):
class Meta:
model = Category
fields = ('id', 'name', 'description',)
class CategoryAdmin(ImportExportModelAdmin):
list_display = ("name",)
search_fields = ["name"]
resource_classes = [CategoryResource]
admin.site.register(Website, WebsiteAdmin)
admin.site.register(ServiceCategory, ServiceCategoryAdmin)
admin.site.register(ProductCategory, ProductCategoryAdmin)
admin.site.register(Category, CategoryAdmin)
On the admin side I see the import and export buttons and I am able to import a file but the data is not getting correctly imported and the only thing getting generated is the id. So for every line in the csv doc the id is generated bu not the name nor the description is being imported.
Can someone tell me what am I doing wrong pls?
Here is the csv file shortened:
eCommerce,A website for online buying and selling products,,
Business,A website for promoting and conducting business activities,,
Blog,A website for publishing articles stories and posts,,
Portfolio,A website showcasing an individual's or organization's work,,
Event,A website for promoting and managing events,,
Personal,A personal website for individuals to share their interests and content,,
Membership,A website with restricted access for members only,,
Nonprofit,A website for nonprofit organisations to promote their cause,,
It could be that your csv is invalid, try this:
fields
the import process will expect it to be there. Note I have picked default ids, but these should match internal ids if you want update logic to work (otherwise it will create duplicates). Since you define 'name' and 'parent' as unique together, you could use these instead of id, although obviously a foreign key reference to 'parent' will need to be in the feed. See docs.Example:
id,name,description
1,eCommerce,A website for online buying and selling products
2,Business,A website for promoting and conducting business activities