確かに新しい質問ではないと思いますが、次のようになります。
私の Django ベースの Order システムでは、各ユーザー (スタッフではない) は、そのユーザーを正しい Customer オブジェクトに一致させる CustomerProfile オブジェクトに関連付けられています。この顧客ユーザーは、ログインして未払いの請求書を表示できます。顧客の請求書を表示するには、次のように移動します。
/請求書/顧客/97/
(顧客請求書 #97)
これは問題ありませんが、認証を組み込む必要があるため、顧客のプロファイルの一部であるユーザーは、たとえば /invoices/customer/92/ を手動で入力して別の顧客の請求書を表示できません (請求書 92 は別の顧客のものです)。
私はこれを持っていますが、それは本当に良いコードではありません (そして動作しません):
def customer_invoice_detail(request, object_id):
user = threadlocals.get_current_user()
try:
userprofile = UserProfile.objects.get(user=user)
user_customer = userprofile.customer.id
except UserProfile.DoesNotExist:
user_customer = None
if (request.user.is_authenticated() and user_customer is not null) or request.user.is_staff():
invoice = CustomerInvoice.objects.get(pk=object_id)
product_list = CustomerInvoiceOrder.objects.filter(invoice=object_id)
context = {
'object': invoice,
'product_list': product_list,
}
return render_to_response("invoices/customer_invoice_detail.html", context, context_instance=RequestContext(request))
else:
return HttpResponse("You are not authorised to view this invoice")
これに対処するためのより良い/より簡単な方法である必要があります-何かアイデアはありますか?
乾杯