how can i copy foreign key data from one object to another?
e.g.
#models.py
class ModelA(models.Model)
field1 = models.CharField(max_length=10)
class ModelB(models.Model)
field2 = models.CharField(max_length=10)
field3 = models.ForeignKey(ModelA)
#views.py
a = ModelA(field1 = 'hello')
b = ModelB(field2 = 'goodbye', field3 = a)
c = ModelB(field2 = 'goodbye again', field3 = a)
d = ModelA(field1 = 'another')
I now want to give d the same foreign key data as a. i.e. records b and c.
Thanks
John
Well, if I understood what you want.
You’ll have to modify your model to:
So, you can do:
Or, if you prefer to keep your model you can do:
Another way I think it can be what you want is:
I think it’s done. But I don’t know, I hope it could be useful to you. ^^