我正面臨OneToOneField的一個非常奇怪的問題。我一直喜歡ValueError:無法分配用戶:我的OneToOneField關係上的問題
class Doctor(models.Model):
user = models.OneToOneField(User)
的問題是一個非常簡單的模型,我在遷移方法RunPython。我寫了一個0002_addusers遷移取決於0001_initial,代碼如下:
class Migration(migrations.Migration):
def create_users(apps, schema_editor):
u = User.objects.create_superuser('admin', '[email protected]', 'admin')
u.save()
du = User.objects.create_user(username='doc01', password='doc01')
du.save()
def create_doctors(apps, schema_editor):
Doctor = apps.get_model('custom_user', 'Doctor')
du = User.objects.get(username='doc01')
d = Doctor(user=du)
d.save()
dependencies = [
('custom_user', '0001_initial')
]
operations = [
migrations.RunPython(create_users),
migrations.RunPython(create_doctors),
]
什麼是很奇怪的,我是,這非常簡單的代碼工作的意見,在外殼的作品,作品無處不在,除了在遷移:)
回溯是如下:
line 23, in create_doctors
d = Doctor(user=du)
...
ValueError: Cannot assign "<User: doc01>": "Doctor.user" must be a "User" instance.
謝謝你很多關於任何支持!
編輯: 我找到了解決方案。我只好打電話給RunPython
migrations.RunPython(create_users,create_doctor)
爲Avinash建議,即使沒有移動類之外的功能。
似乎後續函數必須作爲單個RunPython調用的參數被調用。
嗨,謝謝你的回覆,但它不起作用。這個錯誤與我在帖子中的相同。 ValueError:無法分配「」:「Doctor.user」必須是「用戶」實例。 –
ennioma
剛剛更新了我的答案。嘗試一下。這應該是解決方案。我們無法直接在遷移中導入模型。 試試吧,讓我知道 –
我已經更新了第一篇文章。謝謝Avinash,問題是我自己對RunPython的調用。現在,如果函數在遷移類中,則新的RunPython也可以工作。 – ennioma