django - 字段“id”需要一个数字,但在 Django 中导入数据时得到“ANPKUR0724”
问题描述
我有两个模型,通过外键关联,当我上传具有外键的模型时,自动创建的 id 值采用外键字段的值。可能的原因是什么?
class Station(models.Model):
BS_ID = models.TextField(max_length=20,unique=True)
BS_Name = models.CharField(max_length=20)
City = models.CharField(max_length=20,null=True)
State = models.CharField(max_length=20,null=True)
City_Tier = models.CharField(max_length=10,null=True)
Lat = models.DecimalField(max_digits=10,decimal_places=5,null=True,blank=True)
Long = models.DecimalField(max_digits=10,decimal_places=5,null=True,blank=True)
class CustomerLVPN(models.Model):
Customer_Name = models.CharField(max_length=200)
Order_Type = models.CharField(max_length=200,null=True)
Feasibility_ID = models.IntegerField(null=True)
Circuit_ID = models.CharField(max_length=250,null=True,blank=True)
Req_BW = models.DecimalField(max_digits=6,decimal_places=3,null=True,blank=True)
City = models.CharField(max_length=200,null=True)
State = models.CharField(max_length=200,null=True)
Region =models.CharField(max_length=200,null=True)
L2_Lat = models.DecimalField(max_digits=6,decimal_places=3,null=True,blank=True)
L2_Long = models.DecimalField(max_digits=6,decimal_places=3,null=True,blank=True)
BS_ID = models.ForeignKey(Station,to_field='BS_ID',related_name='ConfirmedCustomer',on_delete=models.SET_NULL,null=True)
因此,当我为 CustomerLVPN 模型上传数据时,我收到以下错误 - “BS_ID 字段 'id' 需要一个数字,但得到了 'ANPKUR0724'。”
以下是资源代码:
class StationResource(resources.ModelResource):
class Meta:
model=Station
exclude = ['id']
use_bulk=True
batch_size = 500
class StationAdmin(ImportExportModelAdmin):
resource_class=StationResource
class LVPNResource(resources.ModelResource):
class Meta:
model = CustomerLVPN
use_bulk = True
batch_size = 500
class LVPNAdmin(ImportExportModelAdmin):
resource_class = LVPNResource
解决方案
对于您LVPNResource
,您需要将 aForeignKeyWidget
与您的bs_id
字段相关联:
bs_id = fields.Field(
column_name='BS_ID',
attribute='BS_ID',
widget=ForeignKeyWidget(Station, 'BS_ID'))
推荐阅读
- java - 如何修复weblogic模块异常问题
- bash - 在 Bash kill 错误中创建新进程后
- visual-studio-code - 仅删除一个标志,而不是所有标志直到最后一个选项卡
- mysql - 如何在一个视图中显示超过 1 个表格
- charts - Highstock/highcharts - yAxis 标题宽度
- python - 使用不和谐机器人更新数据库。为什么机器人不识别和更新数据库?
- azureservicebus - 使用 MassTransit 路由到开发环境
- android - Android Bodymovin 动画转 mp4 视频
- php - 获取由 file_put_contents() 生成的文件的 url;
- javascript - 如何独立设置输入元素的样式