0

Using Widget Many to many when importing the result is empty. Tags do exists in the original model.

See Image.

import result

Excel imported

| provider  | tag             | provider_tag_id |
|-----------|-----------------|-----------------|
| Lionsgate | Planes, Top     |                 |
| FOX       | Houses, Low     |                 |
| Dorcel    | something, else |                 |

Model where the m2m is pointing

class Tag(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=250, unique=True)

    def __str__(self):
        return self.name

Model where the m2m relationship is

class ProviderTag(models.Model):
    provider_tag_id = models.AutoField(primary_key=True)
    provider = models.ForeignKey(Provider, on_delete=models.DO_NOTHING)
    tags = models.ManyToManyField(Tag)

Resource to import

class ProviderTagResource(resources.ModelResource):

provider = fields.Field(
    column_name='provider',
    attribute='provider',
    widget=ForeignKeyWidget(Provider, 'name')
)
tags = fields.Field(
    column_name='tags',
    attribute='tags',
    widget=ManyToManyWidget(Tag, separator=',', field='name')
)

class Meta():
    model = ProviderTag
    fields = ('provider', 'tags', 'provider_tag_id',)
    import_id_fields = ('provider_tag_id',)

Admin.py

 class ProviderTagAdmin(ImportExportModelAdmin):
    list_display = ('provider', 'get_tags', 'provider_tag_id')
    search_fields = ['provider',]
    resource_class = ProviderTagResource

    def get_tags(self, obj):
        print(obj.tags)
        return ", ".join([str(p) for p in obj.tags.all()])
admin.site.register(ProviderTag, ProviderTagAdmin)
Alex Anadon
  • 77
  • 3
  • 11

1 Answers1

0

I cannot provide a direct answer but my suggestion would be to get the example application running.

You can easily test importing a m2m field. You would have to define a m2m field on BookResource in the core/admin.py file:

class BookResource(ModelResource):
    categories = fields.Field(
        attribute='categories',
        widget=widgets.ManyToManyWidget(Category, field='name',
                                        separator='|')
    )

    class Meta:
        model = Book

Then you can import a sample file such as:

id,name,author,author_email,imported,published,published_time,price,categories
1,book1,,email@example.com,0,2020-07-21,,10.25,Category1|Category2

At least if you can prove it is working in the example application, then you can hopefully try to debug your implementation.

Matthew Hegarty
  • 3,791
  • 2
  • 27
  • 42
  • I will Try. Let's see If I find out what's missing. – Alex Anadon Jul 29 '20 at 13:07
  • Ok. The thing is, This is working when I create a CSV from command line. (Like the one you shared ) When I'm using Excel I got an error. – Alex Anadon Jul 29 '20 at 13:45
  • Ok. More info here... Import Export test app have the same issue. :( – Alex Anadon Jul 29 '20 at 13:53
  • It works fine for me using the example above (including xlsx files) – Matthew Hegarty Jul 29 '20 at 14:18
  • Using excel is not working for me. I think is something related to regional configuration. Like list separator on windows control panel. I have it in Spain. I've found a way around to 'my' users. Using Google Drive Spreadhseet and then downoading it as xsl it works... – Alex Anadon Jul 30 '20 at 09:31
  • It seems more related to https://stackoverflow.com/questions/7751157/python-csv-list-separator-based-on-regional-settings. – Alex Anadon Jul 31 '20 at 08:34
  • without specifying separator parameter the default is `","` and specifying separator as above, both are not working on `django-import-export==2.8.0` and `django==4.0` – Nwawel A Iroume Jun 21 '22 at 22:39
  • suggest try django-import-export 3.0 beta and if still a problem raise a github issue – Matthew Hegarty Jun 22 '22 at 12:26
  • @MatthewHegarty I figured out why it is not working on my side. the header name for the m2m had "SPACE" after the `,`. This is weird!!!. do that means that a csv with a space after a colon in the header is not a valid csv? – Nwawel A Iroume Jun 23 '22 at 19:09
  • could you [raise an issue](https://github.com/django-import-export/django-import-export/issues/)? Please give us as much info as you can to reproduce – Matthew Hegarty Jun 24 '22 at 10:40