我想在多对多关系中添加其他字段,我创建了一个名为Contact的中间模型(目标是实现一个允许用户跟随其他用户并被跟踪的系统)。
class Contact(models.Model):
user_from = models.ForeignKey(User,
related_name='rel_from_set')
user_to = models.ForeignKey(User,
related_name='rel_to_set')
created = models.DateTimeField(auto_now_add=True,
db_index=True)
class Meta:
ordering = ('-created',)
def __str__(self):
return '{} follows {}'.format(self.user_from, self.user_to)
我正在使用Django提供的用户模型(来自django.contrib.auth.models)。由于这个模型不是我创建的,如果我想要添加字段,我应该(或者至少,我认为我应该)添加它们dinamically(使用monkey-patch)。所以在models.py文件的末尾我添加了以下代码:
User.add_to_class('following', models.ManyToManyField('self', through=Contact , related_name='followers', symmetrical=False))
但我跑了python manage.py makemigrations
我收到了以下错误:
Migrations for 'auth':
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\base.py", line 345, in execute
output = self.handle(*args, **options)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\commands\makemigrations.py", line 192, in handle
self.write_migration_files(changes)
File "C:\Program Files (x86)\Python35-32\lib\site-packages\django\core\managem
ent\commands\makemigrations.py", line 210, in write_migration_files
migration_string = os.path.relpath(writer.path)
File "C:\Program Files (x86)\Python35-32\lib\ntpath.py", line 574, in relpath
path_drive, start_drive))
ValueError: path is on mount 'C:', start on mount 'D:'
快速谷歌搜索后:
&#34; os.relpath确实为您提供了两个目录之间的相对路径。
您遇到的问题是,在Windows上,如果两个目录位于不同的驱动器上,则相对路径甚至不存在(这正是错误消息所说的)。 &#34;
但是解决方案是什么? 我使用的是Windows 8和Django 1.10。
答案 0 :(得分:1)
您正在从另一个驱动器(而不是安装了django的makemigrations
)运行C:
。
Windows上有makemigrations的错误:
所以要解决这个错误你应该:
C:
驱动器