I am using ImportExportMixin to add the import export buttons to my admin page:
i write it like this :
from django.contrib import admin
from .models import *
from import_export.admin import ImportExportMixin
class PlayerAdmin(ImportExportMixin,admin.ModelAdmin):
list_display = [field.name for field in Player._meta.fields]
admin.site.register(Player,PlayerAdmin)
When I do th export I get something like this :
[{"id": 1, "name": "player1", "date_birth": "1997-01-01", "tshirtnumber": 19, "club": 1, "poste": "ST"}]
The club is a ForeingKey and I want the name of the club not the id. so what to do ? and please I need the same thing for the import I mean I want to import players with their club name .
I am expecting :
[{"id": 1, "name": "player1", "date_birth": "1997-01-01", "tshirtnumber": 19, "club": "chelsea", "poste": "ST"}]
For export you can define the relation in the fields
list of your resource:
class PlayerResource(ModelResource):
class Meta:
model = Player
# add all your export fields
fields = ("id", "name", "club__name")
For importing relations, declare a ForeignKeyWidget
:
class PlayerResource(ModelResource):
# values here might be different depending on your model
club = fields.Field(
column_name='club',
attribute='club',
widget=ForeignKeyWidget(Club, field='name'))
class Meta:
model = Player
# ...