Field 'id' expected a number but got 'ANPKUR0724' while Importing Data in Django

497 views Asked by At

I have two models, related through a Foreign Key, when im uploading the Model having Foreign key , the id value which is auto created is taking value of the Field which is the foreign key. What could be the possible reason?.

 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)

So when im Uploading data for CustomerLVPN model im getting the following error - "BS_ID Field 'id' expected a number but got 'ANPKUR0724'." enter image description here

Below is the Resource Code:

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
1

There are 1 answers

0
Matthew Hegarty On BEST ANSWER

For your LVPNResource you need to associated a ForeignKeyWidget with your bs_id field:

bs_id = fields.Field(
        column_name='BS_ID',
        attribute='BS_ID',
        widget=ForeignKeyWidget(Station, 'BS_ID'))