How to create a triple-join table with Django

后端 未结 4 1477
走了就别回头了
走了就别回头了 2021-02-14 11:25

Using Django\'s built in models, how would one create a triple-join between three models.

For example:

  • Users, Roles, and Events are the models.
  • Us
4条回答
  •  情深已故
    2021-02-14 11:54

    I'd model Role as an association class between Users and Roles, thus,

    class User(models.Model):
         ...
    
    class Event(models.Model):
         ...
    
    class Role(models.Model):
         user = models.ForeignKey(User)
         event = models.ForeignKey(Event)
    

    And enforce the one role per user per event in either a manager or SQL constraints.

提交回复
热议问题