0
我想基於我的詳細視圖中的主鍵過濾對象。有沒有一種方法可以在我的views.py中調用我的主鍵或者我可以按照其他方式進行過濾?這裏是我的代碼:Django DetailView:基於主鍵過濾對象
models.py
class Accounts(models.Model):
account_name = models.CharField(max_length=50)
pricing_id = models.ForeignKey('Pricing')
class OrderRecords(models.Model):
order_id = models.ForeignKey('Orders')
account_id = models.ForeignKey('Accounts')
item_id = models.ForeignKey('Items')
views.py
class AccountDetailView(generic.DetailView):
model = Accounts
template_name = "orders/accountdetail.html"
def get_context_data(self, **kwargs):
context = super(AccountDetailView, self).get_context_data(**kwargs)
context['orderrecords'] = OrderRecords.objects.filter(????????)
return context
更新:
因此,這是我做了改變:
views.py
class AccountDetailView(generic.DetailView):
model = Accounts
template_name = "orders/accountdetail.html"
def get_context_data(self, **kwargs):
pk = self.kwargs['pk']
context = super(AccountDetailView, self).get_context_data(**kwargs)
context['orderrecords'] = OrderRecords.objects.filter(account_id=pk)
return context
好了,所以我用:'PK = self.kwargs [ 'PK']'和工作。謝謝! –