Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 与'的多对多关系;通过';迁移时模型返回错误_Django_Django Models_Django Views - Fatal编程技术网

Django 与'的多对多关系;通过';迁移时模型返回错误

Django 与'的多对多关系;通过';迁移时模型返回错误,django,django-models,django-views,Django,Django Models,Django Views,我正在尝试实现一个系统,该系统允许两个Profile模型对象成为对模型对象的一部分 以下是配置文件,后面是一对模型: class Profile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, bla

我正在尝试实现一个系统,该系统允许两个
Profile
模型对象成为
模型对象的一部分

以下是配置文件,后面是一对模型:

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,
                                on_delete=models.CASCADE,
                                null=True, blank=True)
    pair = models.ManyToManyField('self', through='Pair',
                                   symmetrical=False,
                                   related_name='pair_to+')

class Pair(models.Model):
    requester = models.ForeignKey(Profile, related_name='pairing_requester')
    accepter = models.ForeignKey(Profile, related_name='pairing_accepter')
    requester_learns = models.CharField(max_length=60, null=True)
    requester_teaches = models.CharField(max_length=60, null=True)  
配置文件之间的关系应该是对称的,因此(profile1,profile2)是唯一的对象,我不希望创建(profile2,profile1)

因此,我正试图建立这种关系

makemigrations
时,我收到错误:

ERRORS:
<function ManyToManyField.contribute_to_class.<locals>.resolve_through_model at 0x1044b47b8>: (models.E022) <function ManyToManyField.contribute_to_class.<locals>.resolve_through_model at 0x1044b47b8> contains a lazy reference to user_profile.pair, but app 'user_profile' doesn't provide model 'pair'.
user_profile.Profile.pair: (fields.E331) Field specifies a many-to-many relation through model 'Pair', which has not been installed.
错误:
:(models.E022)包含对user_profile.pair的延迟引用,但应用程序“user_profile”不提供模型“pair”。
user_profile.profile.pair:(fields.E331)字段通过尚未安装的模型“pair”指定多对多关系。
我做错了什么

class Pair(models.Model):
    requester = models.ForeignKey(Profile, related_name='pairing_requester')
    accepter = models.ForeignKey(Profile, related_name='pairing_accepter')
    requester_learns = models.CharField(max_length=60, null=True)
    requester_teaches = models.CharField(max_length=60, null=True)  

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,
                                on_delete=models.CASCADE,
                                null=True, blank=True)
    pair = models.ManyToManyField('self', through='Pair',
                                   symmetrical=False,
                                   related_name='pair_to+')

您所做的错误在于,在调用与另一个模型的关系之前,您需要提及该模型,django将首先创建PAIR模型,然后将该模型安装到另一个模型。因此,在进行迁移之前,请使用此选项删除旧的迁移文件

,这样就没有办法将它们保存在不同的model.py文件中了吗?您可以保留它,但需要先创建表,然后再对其进行分配,否则django也无法理解它将创建与哪个表的关系,由于在创建关系时该表不存在,因此我尝试先删除迁移并迁移配对模型,但收到相同的错误。这与在配置文件模型中将配对模型指定为字符串有关吗?问题在于数据库,我想你需要删除数据库,然后删除迁移文件,然后尝试从第一个开始迁移,希望这能解决你的错误。对不起,你在同一个应用程序中创建了两个模型文件吗??这是行不通的,兄弟,把模型放在同一个models.py文件中,因为一个应用程序只能包含一个models.py文件