Manytomany widget- M2M fields appear blank in export via Admin export
See original GitHub issueI have the following models and M2M relationships:
class Market(models.Model):
marketname = models.CharField(max_length=500)
market_id = models.CharField(max_length=100, blank=True)
def __str__(self):
return self.marketname
class TeamMember(models.Model):
firstname = models.CharField(max_length=100, default= "First Name")
lastname = models.CharField(max_length=100, default= "Last Name")
email = models.EmailField(max_length=254)
def __str__(self):
return self.email
class EmailReport(models.Model):
name = models.CharField(max_length=1000)
recipients = models.ManyToManyField('team.TeamMember', related_name="teamrecipients")
frequency = models.CharField(max_length=1000, blank= True)
markets = models.ManyToManyField('opportunities.Market', blank=True)
def __str__(self):
return self.name
The following is my admin.py for the EmailReport registration.
class EmailReportResource(resources.ModelResource):
recipients = fields.Field(widget=ManyToManyWidget(TeamMember, field="email"))
markets = fields.Field(widget=ManyToManyWidget(Market, field='marketname'))
class Meta:
model = EmailReport
fields = ['id', 'name', 'recipients', 'markets']
class EmailReportAdmin(ImportExportModelAdmin):
resource_class = EmailReportResource
admin.site.register(EmailReport, EmailReportAdmin)
I am able to export- and all char fields export normally- however, my M2M fields all appear blank.
Can anyone please point out my mistake?
Thank you so much!
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Error import m2m widget django import-export - Stack Overflow
The issue is skip_unchanged = True . This may seem unintuitive, but it should read skip_unchanged = False . To understand the reason...
Read more >Django Import Export Tricks & Tips - Dev Genius
Django Import Export Tricks & Tips · Look Up in Foreignkey and ManyToMany fields · Custom Dropdown Widgets · Skip Rows (Avoid Duplicate...
Read more >django-import-export Documentation - Read the Docs
To affect which model fields will be included in an import-export resource ... The ForeignKeyWidget also supports using Django's natural key ...
Read more >django-import-export - Bountysource
Django application and library for importing and exporting data with admin integration. Become a Bounty Hunter You're a Bounty Hunter. View Bounties ...
Read more >django-import-export 数据导入导出 | We all are data. - pointborn
T affect which model fields will be included in an import-export resource, ... appear in the import Result object, and if using the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Same issue.
Update: I solved with the following code (I believe by adding
attribute
to Field)I hope this helps someone dealing with the issue of M2M Fields appearing blank via Admin. I found that when I passed ‘field’ kwarg in the in the ManyToManyWidget(field=‘code’) it would work properly on the export portion (render() method) - but ‘field’ was defaulting to None when on the import. IDK why that was, but this is how I solved it. I created a customM2M widget class from ManyToManyWidget that first helped me diagnose the problem, and then made a new kwarg called ‘m2mField’ that I could pass ‘code’ in a second time - that would both make the export and import function correctly.
models.py
admin.py
custom_widgets.py