我希望能够过滤学生中的电子邮件。 就像我这样做时一样:Student.objects.get(email =“ k@gmail.com”) 学生有email =“ k@gmail.com”的行将被退回。
我尝试使用以下版本,这给了我这个错误: .... ValueError:int()以10为基的无效文字:“ kkk@gmail.com”
我也尝试打印出类型:
views.py
for l in ll:
print(type(l.email.email)). # got <class 'str'>
print(type(l.language_to_learn))
print(type(l.email)) # got <class 'users.models.CustomUser'>
def profile_list(request):
profile=Student.objects.get(email=request.user.email)
return render(request,'view_profile.html',{'profile':profile})
models.py
class Student(models.Model):
email=models.OneToOneField(CustomUser,on_delete=models.CASCADE,primary_key=True,)
language_to_learn=models.CharField(max_length=20,choices=Language_Choices,default='English',)
REQUIRED_FIELDS=['language_to_learn',]
class CustomUser(AbstractUser):
username = None
email = models.EmailField('email address' ,unique=True,blank=False, null=False)
USERNAME_FIELD = 'email'
fullname=models.CharField('Full name',max_length=50,null=True)
REQUIRED_FIELDS = [] # removes email from REQUIRED_FIELDS
objects = UserManager()
答案 0 :(得分:0)
首先,应将models.py email
字段更改为user
而不是email
,以避免混淆。
class Student(models.Model):
user=models.OneToOneField(CustomUser, on_delete=models.CASCADE, primary_key=True)
然后查询类似。
profile=Student.objects.get(user__email=request.user.email)
如果您不想更改字段名称。然后,您应该像在下面那样,因为电子邮件是CustomUser的相关OneToOneField的字段。
profile=Student.objects.get(email__email=request.user.email)