当我使用中间模型时,多对多关系并不是唯一的.

Ken*_*Jaa 2 django many-to-many relation tastypie

我使用"使用直通的ManyToManyField"的中间模型
通常,如果我不使用中间字段,m2m关系将是唯一的并且不能具有重复数据.

我使用中间模型后.m2m之间的关系可以有相同的数据.像这样

|    |    ['0'] (
|    |    |    addToProfile => Array (0)
|    |    |    (
|    |    |    )
|    |    |    endDate =  NULL
|    |    |    feedType =  "N"
|    |    |    id =  1
|    |    |    info =  "Big Kuy No Fear"
|    |    |    likeMaker => Array (3)
|    |    |    (
|    |    |    |    ['0'] =  "/api/v2/user/2/"
|    |    |    |    ['1'] =  "/api/v2/user/2/"
|    |    |    |    ['2'] =  "/api/v2/user/2/"
|    |    |    )
|    |    |    like_count =  "3"
Run Code Online (Sandbox Code Playgroud)

我正在建立一个社交网络.所以这是我的feed对象有3个like_count s.但这三个来自同一个用户"/ api/v2/user/2 /"

我尝试在m2m字段中添加"unique = True"属性,但是django会出现错误,因为它首先没有授予将"unique"属性添加到m2m字段的权限.谁能帮我

sne*_*awo 7

尝试在中间模型中使用unique_together.

class M2MModel(models.Model):
    field1 = models.ForeignKey(Model1)
    field2 = models.ForeignKey(Model2)

    class Meta:
        unique_together = ('field1', 'field2')
Run Code Online (Sandbox Code Playgroud)