这是我的模型。py
class RegistrationOTP(models.Model):
country = models.ForeignKey(Country,on_delete=models.CASCADE, related_name='otp_country', default=1)
phone_number = models.CharField(max_length=13)#, unique=True, validators=[phone_regex],
otp = models.PositiveIntegerField()
created_date = models.DateTimeField(auto_now=True)
is_verified = models.CharField(choices=YES_OR_NO, default='no', max_length=10)
def __unicode__(self):
return '%s : %s'% (self.phone_number, self.otp)
class Country(models.Model):
name = models.CharField(max_length=20, unique=True)
phone_code = models.CharField(max_length=20, unique=True)
这是我的admin.py
class RegistrationOTPResource(resources.ModelResource):
class Meta:
model = RegistrationOTP
fields = ('id','phone_number','otp','created_date','is_verified','country')
def to_representation(self, instance):
rep = super(RegistrationOTPResource, self).to_representation(instance)
rep['country'] = instance.country.name
return rep
class RegistrationOTPAdmin(ExportMixin, admin.ModelAdmin):
resource_class = RegistrationOTPResource
admin.site.register(RegistrationOTP, RegistrationOTPAdmin)
id country phone_number otp created_date is_verified
151 1 6800000000 289320 2018-09-06 10:50:27 yes
150 1 9586773322 660144 2018-09-06 10:50:02 yes
149 1 9653694645 846690 2018-09-06 10:45:23 yes
148 1 6800000000 224465 2018-09-06 10:43:40 yes
147 1 6800000000 386181 2018-09-06 10:41:57 yes
146 1 919829621000 882178 2018-08-21 13:04:39 no
145 1 7584858585 446125 2018-08-21 13:04:39 no
144 1 6112355184 411237 2018-08-21 12:59:31 no
143 1 919829621000 434132 2018-08-21 12:59:25 no
142 1 7584858585 281552 2018-08-21 12:59:25 no
141 1 6112355184 757004 2018-08-21 12:57:38 no
在我的csv国家/地区字段中显示的是国家/地区ID,而不是国家/地区名称。我如何显示国家名称。
请看一下我的代码
答案 0 :(得分:0)
def get_queryset(self, request):
return super(RegistrationOTPResource, self).get_queryset(request).select_related('country')