2

を使用したデータ インポートdjango-import-exportは、m2m 関係で失敗します。

トランザクションはエラーを生成しませんが、オブジェクトの m2m 関係を更新しようとすると、m2m の更新された値は更新されません。

models.py

class Category(MPTTModel):
    name = models.CharField(max_length=155, verbose_name=_('Category'))
    code = models.CharField(max_length=255, verbose_name=_('Category Code'), help_text='primary key value for category', db_index=True)
    categorytype = models.ForeignKey(ProductType, related_name='categories_for', verbose_name=_('Product type'), blank=True, null=True)
    meta_description = models.TextField(verbose_name=_('Meta Description'))
    meta_keywords = models.TextField(verbose_name=_('Meta Keywords'))
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
    slug = models.SlugField(blank=True, verbose_name=_('URL alias'))
    order = models.PositiveIntegerField()

class Product(models.Model):
    reference = models.CharField(max_length=50, verbose_name=_('Reference'), db_index=True)
    name = models.CharField(max_length=255, verbose_name=_('Product Name'))
    description = models.TextField(blank=True, verbose_name=_('Product Description'))
    specs = models.TextField(blank=True, verbose_name=_('Product Specifications'))
    color_code = models.ForeignKey(ColorCode, verbose_name=_('Color Code'), related_name='colorcode_for')
    color_web = models.CharField(max_length=7, verbose_name=_('Web Color'), db_index=True)
    size = models.CharField(max_length=11, verbose_name=_('Product Size'))
    price = models.IntegerField(verbose_name=_('Product Price'))
    price_promo = models.IntegerField(blank=True, null=True, verbose_name=_('Product Promo Price'))
    upc = models.CharField(max_length=155, verbose_name=_('UPC'))
    stock = models.IntegerField(verbose_name=_('Product Stock'))
    categories = models.ManyToManyField(Category, verbose_name=_('Estilo'), related_name='products')
    in_stores = models.BooleanField(default=True, verbose_name=_('Product Availability'), db_index=True)
    try_out = models.BooleanField(default=False, verbose_name=_('Try out Product'))
    pub_date = models.DateTimeField(auto_now_add=True)
    update = models.DateTimeField(auto_now=True)
    slug = models.SlugField(default='', blank=True, verbose_name=_('URL alias'))
    blog = models.URLField(blank=True, verbose_name=_('Blog mention link'))
    parent_product = models.BooleanField(default=False, verbose_name=_('Main Product'), db_index=True)
    main_product = models.ForeignKey('Product', blank=True, null=True, related_name='child_product')
    active = models.BooleanField(default=True, verbose_name=_('Active'), db_index=True)
    trends = models.ManyToManyField(Trends, verbose_name=_('Trends'), blank=True, related_name="trends")
    photoshoot_id = models.CharField(max_length=255, verbose_name=_('Photoshoot ID'), blank=True)
    new_arrival = models.BooleanField(default=False, db_index=True)

resources.py

class ProductResource(resources.ModelResource):
    categories = fields.Field(widget=widgets.ManyToManyWidget(Category, separator="/"))

    def export(self, queryset=None):
        queryset = Product.objects.all()
        headers = self.get_export_headers()
        data = tablib.Dataset(headers=headers)
        for obj in queryset.iterator():
                data.append(self.export_resource(obj))
        return data

    class Meta:
        model = Product
        fields = ('categories', 'reference', 'name_es', 'name_en', 'description_es', 'description_en', 'specs_es', 'specs_en', 'color_code', 'color_web', 'size', 'price_es', 'price_en', 'price_promo_es', 'price_promo_en', 'upc', 'stock', 'in_stores', 'try_out', 'blog', 'active', 'photoshoot_id')
        import_id_fields = ['upc']
        skip_unchanged = True
4

1 に答える 1

2

skip_unchangedメタ オプションを削除する問題が修正されました。クラスのskip_rowメソッドはModelResource、.CSV で割り当てられた新しい値を読み取っていません。

于 2016-01-15T20:01:08.403 に答える