How to make Django ManyToMany relationships explicit on the receiving model's end

前端 未结 2 755
攒了一身酷
攒了一身酷 2021-01-20 18:26

Relationships, particularly ManyToMany, in Django have always bothered me somewhat. In particular, since the relationship is only defined in one of the models,

2条回答
  •  执笔经年
    2021-01-20 19:07

    Found a way on Django's forum (lost link, sorry)

    class Topping(models.Model):
        explicit_pizza_set = models.ManyToManyField(Pizza, through=Pizza.toppings.through, blank=True)
    
    class Pizza(models.Model):
        toppings = models.ManyToManyField(Topping)
    

提交回复
热议问题