Django Bi-directional ManyToMany - How to prevent table creation on second model?

前端 未结 4 779
情书的邮戳
情书的邮戳 2021-01-31 05:51

I have two models, each has a shared ManyToMany, using the db_table field. But how do I prevent syncdb from attempting to create the shared table, for the second model?

4条回答
  •  情书的邮戳
    2021-01-31 06:11

    class Awesome(models.Model):
     one = models.TextField()
     class Meta:
      # Prevent table creation. 
      abstract = True
    

    http://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes

    It's not what you're looking for, but it's the closest they have I belive. Would it not be simpler to make a view?

    Maybe:

    class Awesome(models.Model):
      one = models.CharField(max_length = 255)
      two = models.CharField(max_length = 255)
    
    class AwesomeOne(Awesome):
      fieldOne = models.ForeignKey(User, related_name = 'one')
      class Meta:
       abstract = True
    class AwesomeTwo(Awesome):
      fieldTwo = models.ForeignKey(User, related_name = 'two')
      class Meta:
       abstract = True
    

    Then, you can have one table created and override the __getattr__ to block access to the original fields.

提交回复
热议问题