我有以下型号:
class Developer(models.Model):
COUNTRIES = (
('U', 'United States'),
('O', 'Other'),
)
user = models.OneToOneField(User, related_name='freelancer_user')
country = models.CharField(max_length=1, choices=COUNTRIES, default='U')
class Customer(models.Model):
user = models.OneToOneField(User, related_name='customer_user')
location = models.CharField(max_length=50, blank=True)
我有以下观点:
@login_required
def postjob(request):
job_form = JobCreateForm()
return render(request, 'myapp/postjob.html', {'job_form': job_form})
这对认证起作用。但我只希望Customer用户访问此视图而不是开发人员。我该怎么做?
答案 0 :(得分:0)
您需要user_passes_test
装饰器和测试功能:
@user_passes_test(is_customer)
def postjob(request):
job_form = JobCreateForm()
return render(request, 'myapp/postjob.html', {'job_form': job_form})
is_customer
类似于:
def is_customer(user):
# how do you tell the user is a customer?
if user.is_staff or user.is_superuser:
return False
return True