如何区分具有查找功能的用户
How can I differentiate between users with lookup
我有两个 类 Customer
和 Restaurant
有 OneToOneField
和内置的 django User
。当我转到一个页面时,我试图确定它是哪个 User
。我正在做的事情不起作用,因为 User
模型对于 restaurant
属性总是 return True,所以它永远不会超过第一个 if
语句...
models.py
class Restaurant(models.Model):
restaurant_user = models.OneToOneField(User, on_delete=models.CASCADE)
restaurant_name = models.TextField(max_length=50)
about = models.CharField(max_length=500)
class Customer(models.Model):
customer_user = models.OneToOneField(User, on_delete=models.CASCADE)
about = models.CharField(max_length=500)
views.py
def dashboard(request):
if User.restaurant:
return render(request,'usermanage/dashboard_restaurant.html')
elif User.customer is not None:
return redirect(request, 'usermanage/dashboard.html')
else:
return render(request, 'usermanage/dashboard.html')
您的视图文件正在检查 User
模型而非实例的 restaurant
属性,这不是您在 Django 中使用模型的方式,请阅读 Django's ORM 上的文档为了正确使用。您可以使用 request.user
从请求中访问经过身份验证的用户。你应该这样做:
def dashboard(request):
user = request.user
if user and hasattr(user, 'restaurant'):
return render(request,'usermanage/dashboard_restaurant.html')
elif user and hasattr(user, 'customer') is not None:
return redirect(request, 'usermanage/dashboard.html')
else:
return render(request, 'usermanage/dashboard.html')
请务必阅读有关 Django 的 OneToOneField 和 ORM 的文档。如果你参考 One-to-one relationship documentation 你会读到:
from django.core.exceptions import ObjectDoesNotExist
try:
p2.restaurant
except ObjectDoesNotExist:
print("There is no restaurant here.")
There is no restaurant here.
你也可以使用 hasattr 来避免异常捕获的需要:
hasattr(p2, 'restaurant')
False
这两个示例之一应该是您如何检查您的用户缺少的 restaurant
。
我有两个 类 Customer
和 Restaurant
有 OneToOneField
和内置的 django User
。当我转到一个页面时,我试图确定它是哪个 User
。我正在做的事情不起作用,因为 User
模型对于 restaurant
属性总是 return True,所以它永远不会超过第一个 if
语句...
models.py
class Restaurant(models.Model):
restaurant_user = models.OneToOneField(User, on_delete=models.CASCADE)
restaurant_name = models.TextField(max_length=50)
about = models.CharField(max_length=500)
class Customer(models.Model):
customer_user = models.OneToOneField(User, on_delete=models.CASCADE)
about = models.CharField(max_length=500)
views.py
def dashboard(request):
if User.restaurant:
return render(request,'usermanage/dashboard_restaurant.html')
elif User.customer is not None:
return redirect(request, 'usermanage/dashboard.html')
else:
return render(request, 'usermanage/dashboard.html')
您的视图文件正在检查 User
模型而非实例的 restaurant
属性,这不是您在 Django 中使用模型的方式,请阅读 Django's ORM 上的文档为了正确使用。您可以使用 request.user
从请求中访问经过身份验证的用户。你应该这样做:
def dashboard(request):
user = request.user
if user and hasattr(user, 'restaurant'):
return render(request,'usermanage/dashboard_restaurant.html')
elif user and hasattr(user, 'customer') is not None:
return redirect(request, 'usermanage/dashboard.html')
else:
return render(request, 'usermanage/dashboard.html')
请务必阅读有关 Django 的 OneToOneField 和 ORM 的文档。如果你参考 One-to-one relationship documentation 你会读到:
from django.core.exceptions import ObjectDoesNotExist
try:
p2.restaurant
except ObjectDoesNotExist:
print("There is no restaurant here.")
There is no restaurant here.
你也可以使用 hasattr 来避免异常捕获的需要:
hasattr(p2, 'restaurant')
False
这两个示例之一应该是您如何检查您的用户缺少的 restaurant
。