Python 如何定义django';博士后的多对多领域是什么?

Python 如何定义django';博士后的多对多领域是什么?,python,django,postgresql,Python,Django,Postgresql,我有一个名为Contexts的django模型,它本质上是一个包含用户列表的组。我还有一个名为UserProperty的模型,它包含一些用户信息 class Contexts(models.Model): context_name = models.CharField(max_length=50) context_description = models.TextField() users = models.ManyToManyField(UserProperty , r

我有一个名为
Contexts
的django模型,它本质上是一个包含用户列表的组。我还有一个名为
UserProperty
的模型,它包含一些用户信息

class Contexts(models.Model):
    context_name = models.CharField(max_length=50)
    context_description = models.TextField()
    users = models.ManyToManyField(UserProperty , related_name='context_users')

def getUserImagePath(instance,filename):
    return "/static/images/%s_%s"% (str(time()).replace('.','_'),filename)

class UserProperty(models.Model):
    username = models.CharField(max_length=255, null=False)
    pic = models.ImageField(upload_to=getUserImagePath, default="/static/images/user.png" ,null=True, blank=True)
    org = models.CharField(max_length=255, null=True)
当我使用
django manage.py
运行迁移时,这在我的本地系统中运行得很好。但是在服务器上,由于某些原因,我无法运行迁移,我必须在postgres中手动定义模型(服务器使用django_u.postgres)。所以我就是这么做的

CREATE TABLE webhook_contexts(id integer, context_name character varying(100), context_description character varying(100), users text [], FOREIGN KEY (users) REFERENCES webhook_userproperty);
我不太清楚如何在postgres中复制它。对于
上下文中的
用户
字段
模型,我做了
模型。ManyToManyField(UserProperty,related_name='context_users'))
因此我假设
用户
必须是postgres中的一个数组,其中包含引用表
UserProperty
的字段,即
username
pic
org

.., users text [], FOREIGN KEY (users) REFERENCES webhook_userproperty)
但是我得到了这个错误

ERROR:  there is no primary key for referenced table "webhook_userproperty"

但是我确实为django视为主键的webhook_userproperty定义了一个
id
。那么为什么会出现这个错误?我的表定义是否错误?

多对多不是一个特定的字段:它生成一个额外的表,一个包含两个外键的表:一个指向m2m的“原点”,一个指向“目标”m2m.m2m字段中有一个单独的表,表中有两个外键。但与其尝试手动执行此操作,不如诊断运行迁移的错误。@WillemVanOnsem哦,我明白了。您能告诉我在我的场景中可能会出现什么情况吗?我试图回答这个stackoverflow问题,但我还不知道如何在我的案例中实现它。乍一看,这似乎更像是一个django迁移问题。只有几个问题,您在生产环境中有迁移文件吗?例如:您是否先运行python manage.py makemigrations,然后运行python manage.py migrate?运行命令时是否可以粘贴回溯。@VidyaSagar嘿,实际上这台服务器属于我的公司,运行迁移时存在一些问题。因此,我们现在正在为相应的模型手动定义表。