Answer the question
In order to leave comments, you need to log in
How to make the model refer to itself?
Preamble:
There are 3 models:
Potato, Eggplant, Tomato
And there is one connecting model - Vegetable (it is for the online store catalog, because there can be different goods in the basket, and on the main one, here it is used everywhere, you can say the proxy model ( without proxy attribute),
class Veggies(models.Model):
tomato = models.OneToOneField(Tomato, verbose_name='Томат', on_delete=models.CASCADE, blank=True, null=True)
...
Answer the question
In order to leave comments, you need to log in
All done in one line
gifts = models.ManyToManyField('self', blank=True, symmetrical=False)
If you need to attach a vegetable to a vegetable, then you should think about the fact that a vegetable + a gift is already a different abstraction, which means a different model
Create a second table: id, vegetable_id, vegetable_gift_id. Make vegetable_id and vegetable_gift_id pair unique
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question