Django import export iterate foreign key on object creation

筅森魡賤 提交于 2020-06-29 04:07:12

问题


This is a random model i created to show what im looking for. In this model the store email field is a foriegn key taken from another model which is already populated. I want the stored emails to iterate each time i create an object instead of giving me an option to filter and select.

The exported csv should be like this,

pay    email
10000    xyz@gmail.com
20000    zyx@gmail.com

and so on

Here's the code

class Sample(models.Model):
    pay = models.CharField(max_length=50, blank=False, null=False)
    store_email = models.ForeignKey(DotsStudent, default=1, 
    on_delete=models.SET_DEFAULT)

class SampleResource(resources.ModelResource):
    store_email = Field(column_name='store_email', attribute='store_email', 
                  widget=ForeignKeyWidget(DotsStudent, 'email'))
# store_email = attribute, DotStudent = Model from which the key is inherited from and its attribute name
class Meta:
    model = Sample

class SampleAdmin(ImportExportModelAdmin):
    resource_class = SampleResource

来源:https://stackoverflow.com/questions/62270240/django-import-export-iterate-foreign-key-on-object-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!