def check(request):
if request.user.is_authenticated:
uid = request.user.id
if AllUsers.objects.get(FK_id = uid):
return True
else:
return False
错误:/ users /中的DoesNotExist 匹配查询的AllUsers不存在。
如何解决此错误?
更新 * 型号: *
from django.db import models
from django.contrib.auth.models import User
from mezzanine.pages.models import Page
# The members of Page will be inherited by the Author model, such
# as title, slug, etc. For authors we can use the title field to
# store the author's name. For our model definition, we just add
# any extra fields that aren't part of the Page model, in this
# case, date of birth.
GENDER = (('male','Male'), ('female','Female'))
class AllUsers(models.Model):
FullName = models.CharField(max_length=300)
DOB = models.DateField()
Gender = models.CharField(max_length=7, choices = GENDER)
HomeAddress = models.TextField()
Contact = models.CharField(max_length=300)
Email = models.EmailField()
FK = models.ForeignKey(User)
答案 0 :(得分:1)
您不应该使用_id
属性来检索通过外键连接的模型。
这样做:
user = models.ForeignKey(User)
并且,在您看来:
object_AllUsers = AllUsers.objects.get(user = request.user)
此外,您可能希望使用OneToOneField而不是ForeignKey。 点击此处了解详情:https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey