私は Django 1.11 と Django Registration Redux 1.5 を使用しており、プログラミングは比較的初心者です。
ログインしたユーザーの組織に基づいてデータを表示するアプリケーションを作成しています。
以下のモデルを作成しました。
class Organization(models.Model):
organization_name=models.CharField(max_length=200)
organization_type=models.CharField(max_length=50)
organization_code=models.IntegerField()
organization_krapin=models.CharField(max_length=11)
organization_contactname=models.CharField(max_length=200)
organization_contactnumber=models.IntegerField()
tariff=models.ForeignKey("Tariff", on_delete=models.SET_NULL,
blank=True,
null=True,
)
def __unicode__(self):
return str(self.organization_name)
class OrganizationUserProfile(models.Model):
user=models.OneToOneField(User, unique=True)
organization=models.ForeignKey("Organization")
def __unicode__(self):
return str(self.user)
class OrganizationOrders(models.Model):
REBATE_OPTIONS = (
('Y', 'Yes'),
('N', 'No'),
)
mobile_number=models.IntegerField()
order_status=models.BooleanField(default=False)
scored_data_id=models.CharField(max_length=200, null=True)
order_datetime=models.DateField(default=timezone.now)
order_udatetime=models.DateField(null=True, blank=True)
order_rebate_status=models.CharField(max_length=1, choices=REBATE_OPTIONS, default='N')
organization=models.ForeignKey("Organization")
def __unicode__(self):
return str(self.mobile_number)
そして、ここに私のviews.pyがあります
from __future__ import unicode_literals
from django.shortcuts import render
from .models import OrganizationOrders001, OrganizationUserProfile
def orders(request):
if request.user.is_authenticated:
context = {
'organization_orders': OrganizationOrders.objects.all(),
}
return render(request, 'orders.html', context)
else:
return render(request, 'base.html')
OrganizationUserProfile に登録されているユーザー プロファイルの Organization に基づいて OrganizationOrders をフィルタリングしたいと思います。
次に、組織フィールドが現在ログインしているユーザーの組織フィールドと一致する OrganizationOrders のみを表示します。
私はあなたの答えに感謝します。
クエリのこのフィルターは、あなたが望むことをする必要があります
from __future__ import unicode_literals
from django.shortcuts import render
from .models import OrganizationOrders, OrganizationUserProfile
def orders(request):
if request.user.is_authenticated:
context = {
'organization_orders': OrganizationOrders.objects.filter(organization__organizationuserprofile__user=request.user.pk),
}
return render(request, 'orders.html', context)
return render(request, 'base.html')
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加