我想创建一个有两个manytomanyfield的模型。我不希望Django创建两个关联表,而是为manytomanyfield创建同一个关联表。
这是我的模型:
class Tab(WModel):
forms = models.ManyToManyField('Form', null=True, blank=True)
fields = models.ManyToManyField('Field', null=True, blank=True)
number = models.IntegerField(null=True, blank=True)
class Meta:
db_table = 'tab'
我只想创建这个关联表:
tab_assoc :
id_tab
id_form
id_field
number
class Field(models.Model):
name = models.CharField(max_length=128)
def __str__(self): # __unicode__ on Python 2
return self.name
class Form(models.Model):
name = models.CharField(max_length=128)
fields = models.ManyToManyField(Field, through='Tab')
def __str__(self): # __unicode__ on Python 2
return self.name
class Tab(models.Model):
field = models.ForeignKey(Field)
form = models.ForeignKey(Form)