如何解决-无法在用于指定中介模型的ManyToManyField上设置值。改用catalogue.ProductAttributeValue的Manager

时间:2019-07-21 11:23:33

标签: python django django-oscar django-import-export

我正在使用django-import-export软件包以xls格式导出​​我的产品详细信息。另外,我使用的是django-oscar软件包,尚未修改其任何模型。我正在尝试导入相同的导出的xls文件。但是我遇到了一个奇怪的错误:"ManyToManyField which specifies an intermediary model"。在深入研究django-oscar产品模型后,我发现它已用于模型。我发现了为什么在导入时出现错误,却找不到解决它的线索。谁能指导我该怎么办?

我很难理解django-oscar模型。我仅了解它正在使用称为“ catalogue.ProductAttribute”的模型。我尝试了一些在google和stackoverflow上搜索的解决方案,但没有一个对我有帮助。

我使用了django-import-export提供的before_import_row function(),但这对我没有帮助。

Product = get_model('catalogue', 'Product')

class ProductResource(resources.ModelResource):
    my_categories = fields.Field(column_name='categories')

    def dehydrate_my_categories(self, product):
        categories = Category.objects.filter(product=product)
        result = ''
        for c in categories:
            result += c.
        return '%s' % result

    def before_import_row(self, row, **kwargs):
        product = row['product_class']
        attribute = row['attribute']
        ProductAttribute.objects.create(product_class=product, attribute=attribute)  # create object place

    class Meta:
        model = Product
        fields = (
            'id', 'structure', 'upc', 'parent', 'title', 'slug', 'description', 'product_class__name', 'attributes',
            'product_options', 'recommended_products', 'rating', 'date_created', 'date_updated', 'my_categories',
            'is_discountable')
        export_order = (
            'id', 'structure', 'upc', 'parent', 'title', 'slug', 'description', 'product_class__name', 'attributes',
            'product_options', 'recommended_products', 'rating', 'date_created', 'date_updated', 'my_categories',
            'is_discountable')
        # skip_unchanged = True


class ProductAdmin(ImportExportModelAdmin):
    resource_class = ProductResource

我无法导入相同的导出的.xls文件。

0 个答案:

没有答案