我尝试通过继承 AbstractUser 来扩展 User 模型:
模型.py
class Teacher(AbstractUser):
name = models.CharField(max_length=20)
surname = models.CharField(max_length=20)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE,
related_name='teacher_subject')
person = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
设置.py
AUTH_USER_MODEL = 'education.Teacher'
表格.py
class TeacherRegistrationForm(UserCreationForm):
class Meta(UserCreationForm):
model = Teacher
fields = ('name','surname','subject')
class TeacherChangeForm(UserChangeForm):
class Meta:
model = Teacher
fields = ('name','surname','subject')
管理员.py
class TeacherUserAdmin(UserAdmin):
add_form = TeacherRegistrationForm
form = TeacherChangeForm
model = Teacher
list_display = ['name','surname','subject']
admin.site.register(Teacher, TeacherUserAdmin)
当我尝试创建迁移时,我得到了错误
courses.Course.owner: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
courses.Course.students: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
courses.File.owner: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
courses.Image.owner: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
courses.Text.owner: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
courses.Video.owner: (fields.E301) Field defines a relation with the model 'auth.User', which has been swapped out.
HINT: Update the relation to point at 'settings.AUTH_USER_MODEL'.
我该怎么办?
删除所有迁移,基础。复制我的代码,粘贴到你的。进行迁移。
settings.py
你说的是forms.py
对的,和你的问题无关。显然,该错误特别是在以前的迁移中。该示例开始 - 有效。(请注意,我从您的老师那里简化(删除)了一个字段)