gpt4 book ai didi

python - django 导入导出不适用于 mptt

转载 作者:行者123 更新时间:2023-12-03 08:49:10 25 4
gpt4 key购买 nike

我在尝试让导入导出适用于 mptt 模型时遇到问题。这是我的代码:

models.py

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

class MyModel(MPTTModel):
name = models.CharField(max_length=255)
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
active = models.BooleanField(default=True,null=False,blank=False)
sort_order = models.PositiveIntegerField(default=0,null=True,blank=True)

class MPTTMeta:
order_insertion_by = ['name']

class Meta:
ordering = ['sort_order']

def __str__(self):
return self.name

admin.py

from django.contrib import admin
from mptt.admin import MPTTModelAdmin, DraggableMPTTAdmin
from .models import MyModel
from import_export import resources
from import_export.admin import ImportExportModelAdmin, ImportExportMixin

class MyModelResource(resources.ModelResource):

class Meta:
model = MyModel
exclude = ('lft','rght','tree_id','level')

@admin.register(MyModel)
class MyModelAdmin(ImportExportMixin, DraggableMPTTAdmin):
resource_class = MyModelResource
list_display = ('tree_actions','indented_title','active','sort_order')
list_editable = ('active','sort_order')
list_display_links = ('indented_title',)

当我导出时,它将名称字段放在父列中。当我再次导入时,它在父字段上失败:

"MyModel.parent" must be a "MyModel" instance.

非常感谢任何帮助

最佳答案

我遇到了同样的问题,并按照博丹的问题设法解决了这个问题:Django call 'id' expected a number but got string 。以下是我为使其发挥作用所做的工作:

模型.py

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

# Create your models here.

class Category(MPTTModel):
name = models.CharField(max_length = 120, unique = True)
parent = TreeForeignKey('self', on_delete = models.CASCADE, null = True, blank = True, related_name = "children")

class MPTTMeta:
order_insertion_by = ['name']

class Meta:
verbose_name_plural = 'categories'

def __str__(self):
return self.name

admin.py

from django.contrib import admin
from mptt.admin import DraggableMPTTAdmin, TreeRelatedFieldListFilter
from import_export import resources, fields, widgets

from import_export.admin import ImportExportModelAdmin
from .models import Category

# Resource Classes
class CategoryResource(resources.ModelResource):

parent = fields.Field(
column_name='parent',
attribute='parent',
widget = widgets.ForeignKeyWidget(Category, 'name'))

class Meta:
model = Category
skip_unchanged = True
report_skipped = True
exclude = ('id',)
import_id_fields = ('name',)
fields = ('parent','name','lft','rght','tree_id','level')

# Admin Classes

@admin.register(Category)
class CategoryAdmin(ImportExportModelAdmin,DraggableMPTTAdmin):
resource_class = CategoryResource

所以在你的情况下,我相信你的领域是:

fields = ('parent','name','active','sort_order','lft','rght','tree_id','level')

似乎我们需要排除“id”键,但我不完全确定为什么。我注意到,当我导入模型以前没有见过的类别名称时,它会自行配置 mptt 结构。

我正在使用 Django 3.0.7、django-mptt 0.11.0 和 django-import-export 2.2.0。我是堆栈溢出的新手,因此如果这不是正确的响应方式,我深表歉意,但我希望它能有所帮助!

关于python - django 导入导出不适用于 mptt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60003462/

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