update view doesn't load item value

Hi,
I have the table Well_linedata and it has a ForeignKey, so when I added the to_field argument
the updating view doesn’t show the selected item value

class Well_linedata(models.Model):
    WellID     = models.ForeignKey(Wellinfo ,to_field='WellID', on_delete=models.CASCADE)
    W_MFD      = models.ForeignKey(Manifo_types , on_delete=models.CASCADE)
    L_Diamtr   = models.ForeignKey(Flow_Network ,to_field='Diameter', on_delete=models.CASCADE)

see this image.

so I deleted the to_field from this and changed the values in the models.py.

class Well_linedata(models.Model):
    WellID     = models.ForeignKey(Wellinfo , on_delete=models.CASCADE)
    W_MFD      = models.ForeignKey(Manifo_types , on_delete=models.CASCADE)
    L_Diamtr   = models.ForeignKey(Flow_Network , on_delete=models.CASCADE)

the data was like this in the table and I changed it manually like this

So everything works well.

the problem is that I don’t want to change the model.py tables and change the other data manually in tables I have a lot of data.

thanks

It appears to me that you may be conflating two completely separate topics.

The to_field parameter for a ForeignKey is available to create a foreign key to something other than the primary key of a model - something that is typically very rare.

What you’re trying to do is populate the display within a widget on a form. This has nothing to do with how your data should be modeled.

What you’re really looking for is the to_field_name parameter in your form field.

1 Like