Home>

Thank you for your support.

We are creating a soccer game player with the aim of creating it.

Currently, in order to import CSV with django-import-export,
We are creating CSV and setting the contents of admin.

In using django-import-export this time,
The contents of admin have been set with reference to the following documents.

References
https://tech.fragment.co.jp/python/django/django-import-export/
https://qiita.com/d_kvn/items/463787f80ea906e955b1

Probably due to an error in the Foreign Key and ManyToMany admin statements.
I think the following error has occurred, but
Even if you change the CSV column items and admin column names,
Since the same error has occurred, at what point the error is occurring,
If there is a known method, I would appreciate it if you could teach me.

Thank you.

Error details

Traceback (most recent call last): File "/Users/mm/e_football2022/django/e_football2022/myvenv/lib/python3.8/site-packages/import_export/resources.py", line 676, in import_row
    diff= self.get_diff_class () (self, original, new) File "/Users/mm/e_football2022/django/e_football2022/myvenv/lib/python3.8/site-packages/import_export/resources.py", line 227, in __init__
    self.left= self._export_resource_fields (resource, instance) File "/Users/mm/e_football2022/django/e_football2022/myvenv/lib/python3.8/site-packages/import_export/resources.py", line 248, in _export_resource_fields
    return [resource.export_field (f, instance) if instance else "" for f in resource.get_user_visible_fields ()] File "/Users /mm /e_football2022 /django /e_football2022 /myvenv /lib /python3.8 /site-packages /import_export /resources.py ", line 248, in <
listcomp >
    return [resource.export_field (f, instance) if instance else "" for f in resource.get_user_visible_fields ()] File "/Users /mm /e_football2022 /django /e_football2022 /myvenv /lib /python3.8 /site-packages /import_export /resources.py ", line 888, in export_field
    return field.export (obj) File "/Users/mm/e_football2022/django/e_football2022/myvenv/lib/python3.8/site-packages/import_export/fields.py", line 122, in export
    value= self.get_value (obj) File "/Users/mm/e_football2022/django/e_football2022/myvenv/lib/python3.8/site-packages/import_export/fields.py", line 87, in get_value
    value= getattr (value, attr, None) File "/Users/mm/e_football2022/django/e_football2022/myvenv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 536 , in __get__
    return self.related_manager_cls (instance) File "/Users/mm/e_football2022/django/e_football2022/myvenv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 851, in __init__
    raise ValueError ('"% r" needs to have a value for field "% s" before'File "/Users /mm /e_football2022 /django /e_football2022 /myvenv /lib /python3.8 /site-packages /django /db /models /base.py ", line 519, in __repr__
    return'<
% s:% s >
'% (self.__class__.__name__, self)
TypeError: __str__ returned non-string (type None Type)

CSV content

Import screen

models.py

class RarityCategory (models.Model):
  rarity= models.CharField ('Rarity', max_length= 100, blank= True, null= True)
  def __str__ (self):
        return str (self.rarity)
class Position Category (models.Model):
  position= models.CharField ('position group', max_length= 100, blank= True, null= True)
  def __str__ (self):
        return str (self.position)
class ClubCategory (models.Model):
  clubname= models.CharField ('club group', max_length= 100, blank= True, null= True)
  def __str__ (self):return str (self.clubname) class LeagueCategory (models.Model):
  leaguename= models.CharField ('league group', max_length= 100, blank= True, null= True)
  def __str__ (self):
        return str (self.leaguename)
class PlayStyleCategory (models.Model):
  playstyle= models.CharField ('playstyle group', max_length= 30, blank= True, null= True)
  def __str__ (self):
        return str (self.playstyle)
class Skill (models.Model):
    player_skill= models.CharField ('skill name', max_length= 20, blank= True, null= True)
    def __str__ (self):
        return str (self.player_skill)
class Player (models.Model):
    new_date_field= models.DateTimeField ('release date', blank= True, null= True)
    initial= models.IntegerField ('initial total value', blank= True, null= True)
    maximum= models.IntegerField ('maximum total value', blank= True, null= True)
    level= models.IntegerField ('maximum level', blank= True, null= True)
    player_name= models.CharField ('player name', max_length= 50, blank= True, null= True)
    country= models.CharField ('country name', max_length= 30, blank= True, null= True)
    age= models.IntegerField ('age', blank= True, null= True)
    height= models.IntegerField ('height', blank= True, null= True)
    dominant_foot= models.CharField ('dominant foot', max_length= 2, blank= True, null= True)
    player_image= models.ImageField (upload_to='images', verbose_name='player image', null= True, blank= True)
    position_group= models.ForeignKey (PositionCategory, verbose_name='position group', on_delete= models.PROTECT)
    rarity_group= models.ForeignKey (RarityCategory, verbose_name='Rarity', on_delete= models.PROTECT)
    club_group= models.ForeignKey (ClubCategory, verbose_name='club group', on_delete= models.PROTECT)
    league_group= models.ForeignKey (LeagueCategory, verbose_name='league group', on_delete= models.PROTECT)
    playstyle_group= models.ForeignKey (PlayStyleCategory, verbose_name='playstyle group', on_delete= models.PROTECT)
    skill= models.ManyToManyField (Skill, verbose_name='skill', blank= True)
    def __str__ (self):
        return self.player_name

admin.py

from import_export import fields, resources
from import_export.admin import ImportExportModelAdmin
from import_export.resources import ModelResource
from import_export.widgets import ForeignKeyWidget, ManyToManyWidget
from import_export.fields import Field
class PositionCategoryResource (resources.ModelResource):
    class Meta:
        model= PositionCategory
# Create a class that inherits ModelResource to integrate into the Cost model
class ClubCategoryResource (resources.ModelResource):
    class Meta:
        model= ClubCategory
class LeagueCategoryResource (resources.ModelResource):
    class Meta:
        model= LeagueCategory
class RarityCategoryResource (resources.ModelResource):
    class Meta:
        model= RarityCategory
# Create a class that inherits ModelResource to integrate with the Shop model
class PlayStyleCategoryResource (resources.ModelResource):
    class Meta:
        model= PlayStyleCategory
# Create a class that inherits ModelResource to integrate into the Cost model
class SkillResource (resources.ModelResource):
    class Meta:
        model= Skill
class PlayerResource (resources.ModelResource):
    position_group= Field (attribute='position_group', column_name='position_group_id', widget= ForeignKeyWidget (PositionCategory,'id'))
    rarity_group= Field (attribute='rarity_group', column_name='rarity_group_id', widget= ForeignKeyWidget (RarityCategory,'id'))
    club_group= Field (attribute='club_group', column_name='club_group_id', widget= ForeignKeyWidget (ClubCategory,'id'))
    league_group= Field (attribute='league_group', column_name='league_group_id', widget= ForeignKeyWidget (LeagueCategory,'id'))
    playstyle_group= Field (attribute='playstyle_group', column_name='playstyle_group_id', widget= ForeignKeyWidget (PlayStyleCategory,'id'))
    skill= Field (attribute='skill', column_name='skill_id', widget= ManyToManyWidget (Skill, field='id'))
    class Meta:
        model= Playerimport_order= ('id','new_date_field','initial','maximum','level','player_name','country','age','height','dominant_foot','player_image','position_group' ,'rarity_group','club_group','league_group','playstyle_group','skill') import_id_fields= ['id']
@ admin.register (RarityCategory)
# Create an admin class that inherits ImportExportModelAdmin
class RarityCategoryAdmin (ImportExportModelAdmin):
    ordering= ['id']
    list_display= ('id','rarity')
    # Set resource_class to a class that inherits ModelResource
    resource_class= RarityCategoryResource
@ admin.register (PositionCategory)
# Create an admin class that inherits ImportExportModelAdmin
class PositionCategoryAdmin (ImportExportModelAdmin):
    ordering= ['id']
    list_display= ('id','position')
    # Set resource_class to a class that inherits ModelResource
    resource_class= PositionCategoryResource
@ admin.register (ClubCategory)
# Create an admin class that inherits ImportExportModelAdmin
class ClubCategoryAdmin (ImportExportModelAdmin):
    ordering= ['id']
    list_display= ('id','clubname')
    # Set resource_class to a class that inherits ModelResource
    resource_class= ClubCategoryResource
@ admin.register (LeagueCategory)
# Create an admin class that inherits ImportExportModelAdmin
class LeagueCategoryAdmin (ImportExportModelAdmin):
    ordering= ['id']
    list_display= ('id','leaguename')
    # Set resource_class to a class that inherits ModelResource
    resource_class= LeagueCategoryResource
@ admin.register (PlayStyleCategory)
# Create an admin class that inherits ImportExportModelAdmin
class PlayStyleCategoryAdmin (ImportExportModelAdmin):
    ordering= ['id']
    list_display= ('id','playstyle')
    # Set resource_class to a class that inherits ModelResource
    resource_class= PlayStyleCategoryResource
@ admin.register (Skill)
# Create an admin class that inherits ImportExportModelAdmin
class SkillAdmin (ImportExportModelAdmin):
    ordering= ['id']
    list_display= ('id','player_skill')
    # Set resource_class to a class that inherits ModelResource
    resource_class= SkillResource
@ admin.register (Player)
# Create an admin class that inherits ImportExportModelAdmin
class PlayerImportAdmin (ImportExportModelAdmin):
    ordering= ['id']
    list_display= ('id','new_date_field','initial','maximum','level','player_name','country','age','height','dominant_foot','player_image','position_group' ,'rarity_group','club_group','league_group','playstyle_group','skill')
    filter_horizontal= ('skill',)
    # Set resource_class to a class that inherits ModelResource
    resource_class= PlayerResource
    def skill (self, row):
        return','.join ([x.player_skill for x in row.meta.all ()])
  • Answer # 1

    At the moment, but
    By changing the settings of ForeignKeyWidget to the following contents,
    The import itself was successful.

    class PlayerResource (resources.ModelResource):
    position_group= Field (attribute='position_group', column_name='position_group_id', widget= ForeignKeyWidget (PositionCategory))
    rarity_group= Field (attribute='rarity_group', column_name='rarity_group_id', widget= ForeignKeyWidget (RarityCategory))
    club_group= Field (attribute='club_group', column_name='club_group_id', widget= ForeignKeyWidget (ClubCategory))
    league_group= Field (attribute='league_group', column_name='league_group_id', widget= ForeignKeyWidget (LeagueCategory))
    playstyle_group= Field (attribute='playstyle_group', column_name='playstyle_group_id', widget= ForeignKeyWidget (PlayStyleCategory))
    skill= Field (attribute='skill', column_name='skill_id', widget= ManyToManyWidget (Skill))
    class Meta:
        model= Player
        fields= ('id','new_date_field','initial','maximum','level','player_name','country','age','height','dominant_foot','player_image','position_group' ,'rarity_group','club_group','league_group','playstyle_group','skill')
        import_id_fields= ['id']
    

    However, the new_date_field and skill ManyToManyWidget cannot be imported.
    I hope I can continue to work on self-solving, so
    Once this question has this answer, I would like to resolve it.

    If there are any points that cannot be solved by the above two points, I would appreciate it if you could ask a question.
    Thank you.

  • Answer # 2

    At the moment, but
    By changing the settings of ForeignKeyWidget to the following contents,
    The import itself was successful.

    class PlayerResource (resources.ModelResource):
    position_group= Field (attribute='position_group', column_name='position_group_id', widget= ForeignKeyWidget (PositionCategory))
    rarity_group= Field (attribute='rarity_group', column_name='rarity_group_id', widget= ForeignKeyWidget (RarityCategory))
    club_group= Field (attribute='club_group', column_name='club_group_id', widget= ForeignKeyWidget (ClubCategory))
    league_group= Field (attribute='league_group', column_name='league_group_id', widget= ForeignKeyWidget (LeagueCategory))
    playstyle_group= Field (attribute='playstyle_group', column_name='playstyle_group_id', widget= ForeignKeyWidget (PlayStyleCategory))
    skill= Field (attribute='skill', column_name='skill_id', widget= ManyToManyWidget (Skill))
    class Meta:
        model= Player
        fields= ('id','new_date_field','initial','maximum','level','player_name','country','age','height','dominant_foot','player_image','position_group' ,'rarity_group','club_group','league_group','playstyle_group','skill')
        import_id_fields= ['id']
    

    However, the new_date_field and skill ManyToManyWidget cannot be imported.
    I hope I can continue to work on self-solving, so
    Once this question has this answer, I would like to resolve it.

    If there are any points that cannot be solved by the above two points, I would appreciate it if you could ask a question.
    Thank you.