Is there a way to make one model be able to foreignkey to more than one model? For example
JavaScript
x
7
1
class Tshirt(models.Model):
2
..
3
class Jeans(models.Model):
4
..
5
class Clothes(models.Model):
6
item = ForeignKey(Tshirt and Jeans, on_delete = models.CASCADE)
7
Advertisement
Answer
So you want to connect both the model jeans and shirt with cloth so you an connect like that I am afraid that’s not possible which you are trying but you can connect both model like that
JavaScript
1
9
1
class Tshirt(models.Model):
2
..
3
class Jeans(models.Model):
4
..
5
class Clothes(models.Model):
6
item_one = models.ForeignKey(Tshirt, on_delete = models.CASCADE)
7
item_two = model.ForeignKey(Jeans, on_delete = models.CASCADE)
8
item = GenericForeignKey('item_one', 'item_two')
9
or the second way is connect the cloth with both the model
JavaScript
1
7
1
class Tshirt(models.Model):
2
cloth = models.ForeignKey(Clothes, on_delete = models.CASCADE)
3
class Jeans(models.Model):
4
cloth = models.ForeignKey(Clothes, on_delete = models.CASCADE)
5
class Clothes(models.Model):
6
.
7
I know this is not the answer you were expecting but according to me this is the only ways two interconnect model