导入 ManyToMany 字段
Import ManyToMany field
我的模型中有一个 ManyToMany 字段。
我想导入数据并输入该字段。
我的Resources.py:-
class ClinicResource(resources.ModelResource):
language = fields.Field(column_name='language',attribute='language', widget=ForeignKeyWidget(Language, 'code'))
country = fields.Field(column_name='country',attribute='country', widget=ClinicCountryForeignKeyWidget(model = Country, field ='name'))
clinic_languages = fields.Field(widget=ManyToManyWidget(ClinicLanguages, field='name'))
class Meta:
model = Clinic
fields = ('code', 'name', 'city', 'score')
exclude = ('id',)
import_id_fields = ('code', 'name', 'city', 'score', 'language', 'country')
我的Models.py:-
class Clinic(models.Model):
code = models.CharField(max_length= 10, blank= False, null= False)
name = models.CharField(max_length= 256, blank= False, null= False)
slug = models.SlugField(null= True, blank= True)
# country = models.CharField(max_length= 256, blank= False, null= False)
city = models.CharField(max_length= 256, blank= False, null= False)
country = models.ForeignKey(Country, on_delete=models.CASCADE, related_name='allcliniccountry', blank= True, null=True)
score = models.FloatField(blank= False, null= False, default= 2, validators=[MinValueValidator(min), MaxValueValidator(max)])
language = models.ForeignKey(Language, on_delete=models.CASCADE, related_name='allclinicslanguage', blank= True)
# about = models.TextField(blank= True, null= True)
clinic_languages = models.ManyToManyField(ClinicLanguages, related_name='clinic_language', blank= True)
about = tinymce_models.HTMLField(blank= True, null= True)
created= models.DateTimeField(auto_now=True)
status = models.CharField(max_length= 30, choices= SIZE_CHOICES, default= 'pending', blank= False, null= False)
注意 clinic_languages 字段,这是我要导入的内容
您可以在 django-import-export source 中查看 ManyToManyWidget
的工作原理。
您已经正确定义了您的字段:
clinic_languages = fields.Field(widget=ManyToManyWidget(ClinicLanguages, field='name'))
这意味着查找代码将尝试使用 'name' 字段加载相关实例。默认情况下,它们必须在您的导入文件中以“,”分隔,例如:
clinic_languages
name1,name2,name3
如有必要,您可以覆盖分隔符。
我的模型中有一个 ManyToMany 字段。
我想导入数据并输入该字段。
我的Resources.py:-
class ClinicResource(resources.ModelResource):
language = fields.Field(column_name='language',attribute='language', widget=ForeignKeyWidget(Language, 'code'))
country = fields.Field(column_name='country',attribute='country', widget=ClinicCountryForeignKeyWidget(model = Country, field ='name'))
clinic_languages = fields.Field(widget=ManyToManyWidget(ClinicLanguages, field='name'))
class Meta:
model = Clinic
fields = ('code', 'name', 'city', 'score')
exclude = ('id',)
import_id_fields = ('code', 'name', 'city', 'score', 'language', 'country')
我的Models.py:-
class Clinic(models.Model):
code = models.CharField(max_length= 10, blank= False, null= False)
name = models.CharField(max_length= 256, blank= False, null= False)
slug = models.SlugField(null= True, blank= True)
# country = models.CharField(max_length= 256, blank= False, null= False)
city = models.CharField(max_length= 256, blank= False, null= False)
country = models.ForeignKey(Country, on_delete=models.CASCADE, related_name='allcliniccountry', blank= True, null=True)
score = models.FloatField(blank= False, null= False, default= 2, validators=[MinValueValidator(min), MaxValueValidator(max)])
language = models.ForeignKey(Language, on_delete=models.CASCADE, related_name='allclinicslanguage', blank= True)
# about = models.TextField(blank= True, null= True)
clinic_languages = models.ManyToManyField(ClinicLanguages, related_name='clinic_language', blank= True)
about = tinymce_models.HTMLField(blank= True, null= True)
created= models.DateTimeField(auto_now=True)
status = models.CharField(max_length= 30, choices= SIZE_CHOICES, default= 'pending', blank= False, null= False)
注意 clinic_languages 字段,这是我要导入的内容
您可以在 django-import-export source 中查看 ManyToManyWidget
的工作原理。
您已经正确定义了您的字段:
clinic_languages = fields.Field(widget=ManyToManyWidget(ClinicLanguages, field='name'))
这意味着查找代码将尝试使用 'name' 字段加载相关实例。默认情况下,它们必须在您的导入文件中以“,”分隔,例如:
clinic_languages
name1,name2,name3
如有必要,您可以覆盖分隔符。