How to create 3 way ManyToMany relationship django

后端 未结 1 1643
借酒劲吻你
借酒劲吻你 2021-01-16 04:40

Need to model the relationship between servers, applications, and server role, in django admin.

  • Server can have one or many applicatio
相关标签:
1条回答
  • 2021-01-16 05:09

    If I have understood your requirement correctly, this is more like what you need

    class ServerRole(models.Model):
        name = models.CharField(max_length=200)
    
    class Application(models.Model):
        name = models.CharField(max_length=200)
    
        def __unicode__(self):
            return self.name
    
    class Server(models.Model):
        name = models.CharField(max_length=200)
        apps = models.ManyToMany('Application', through='ServerRole', related_name='servers')
    

    Rational - A server can have many apps and an app can have many servers. if an app is on a particular server it should have only one role on it (if not your whole system because very much more complicated and harder to write) which can be implemented by the through model in a Many to Many relationship.

    0 讨论(0)
提交回复
热议问题