我想添加額外的字段到多對多的關係,我創建了一個名爲Contact的中間模型(目標是實現一個系統,允許用戶關注其他用戶並遵循)。Django 1.10路徑掛載'C:',在掛載'D'開始:'
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(與猴子補丁)添加它們。因此,在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:'
一個快速谷歌搜索後:
「os.relpath的確給你兩個目錄之間的相對路徑
你遇到的問題是,在Windows上,如果兩個目錄位於不同的驅動器上(這正是錯誤消息所說的)。 「
但如何解決? 我使用Windows 8和Django的1.10。
是什麼讓你認爲錯誤與添加的字段有關?無論如何,你應該展示完整的回溯。 –
@DanielRoseman完整追蹤發佈。錯誤發生後,我添加了上面的代碼,所以我很積極,這是問題所在。 – ROBlackSnail