错误: 1)user.Login.password:(fields.E304)“Login.password”的反向访问者与“Login.username”的反向访问者冲突。 2)user.Login.username :( fields.E304)“Login.username”的反向访问者与“Login.password”的反向访问者冲突。
models.py
from django.db import models
from django.core.urlresolvers import reverse
class User(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
username = models.CharField(max_length=100)
password = models.CharField(max_length=50)
confirm_password = models.CharField(max_length=50)
email = models.EmailField(max_length=100)
position = models.CharField(max_length=50)
def get_absolute_url(self):
return reverse('user:register', kwargs={'pk': self.pk})
class Login(models.Model):
username = models.ForeignKey(User, on_delete=models.CASCADE)
password = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
login = {'username': self.username, 'password': self.password}
return login
答案 0 :(得分:2)
您可以使用related_name
属性
class Login(models.Model):
username = models.ForeignKey(User, on_delete=models.CASCADE, related_name="username_users")
password = models.ForeignKey(User, on_delete=models.CASCADE, related_name="password_users")
但是我没有理由让你的密码和用户名都与用户有fk关系!!!