I don't understand how django-import-export module deals with ForeignKeys.
Here is a simple exemple :
models.py
class TFamilies(models.Model):
id_fam = models.AutoField(primary_key=True, unique=True)
name_fam = models.CharField(max_length=1024, blank=True,verbose_name='Famille')
class TGenus(models.Model):
id_genus = models.AutoField(primary_key=True, unique=True)
name_genus = models.CharField(max_length=1024,verbose_name='nom de genre')
id_fam = models.ForeignKey(TFamilies, null=True, db_column='id_fam', blank=True, verbose_name='Famille')
I would like to allow people adding genus with family associated ! A CSV/XLS with only name_genus and name_fam... (and id left blank).
Family already exist in DB most of the time, Django juste have to find the right id number...
admin.py
class TGenusResource(resources.ModelResource):
name_fam = fields.Field(widget=widgets.ForeignKeyWidget(TFamilies, 'name_fam'))
class Meta:
model = TGenus
import_id_fields = ['id_genus']
class TGenusAdmin(ImportExportActionModelAdmin):
form = TGenusAdminForm
resource_class = TGenusResource
pass
This configuration lead to error in import interface :
Line number: 1 - 'NoneType' object has no attribute 'name_fam'
Traceback (most recent call last):
File "/....../lib/python2.7/site-packages/import_export/resources.py", line 348, in import_data
row_result.object_repr = force_text(instance)
File "......./lib/python2.7/site-packages/django/utils/encoding.py", line 85, in force_text
s = six.text_type(s)
AttributeError: 'NoneType' object has no attribute 'name_fam'
I don't understand...
I also tried answer there : django-import-export resource definition for foreignkey field?
and sort of like there : Foreign Key in django migration using django-import-export
Do I have to use before_import to find myself the match ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…