我有以下型号:
class Rate(models.Model):
building = models.ForeignKey(Building, verbose_name="Objekt")
year = models.IntegerField("Jahr")
monthly_rate = models.DecimalField("Monatsrate", max_digits=8, decimal_places=2)
paid = models.DecimalField("Tatsächlich Bezahlt (Brutto)", max_digits=8, decimal_places=2, blank=True, null=True)
class Building(models.Model):
customer = models.ForeignKey(Customer, verbose_name="Kunde")
...
billing_begin = models.DateField("Abrechnungsbeginn")
class Bank(models.Model):
name = models.CharField("Bankname", max_length=64) #Bankname
account_number = models.IntegerField("Kontonummer") #Kontonummer
code_number = models.IntegerField("Bankleitzahl") #Bankleitzahl
IBAN = models.CharField(max_length=32)
BIC = models.CharField(max_length=32)
class Customer(models.Model):
bank = models.ForeignKey(Bank)
现在我有以下观点:
class BuildingRateListView(ListView):
model = Building
template_name = "Building/building_bank_list.html"
def get_context_data(self, **kwargs):
context = super(BuildingRateListView, self).get_context_data(**kwargs)
buildings = Building.objects.filter(customer__debitor=True).order_by('customer__bank', 'customer__last_name')
erg = []
for b in buildings:
rate = str(b.rate_set.latest('year')) + "€"
erg.append(rate)
context['building_list'] = erg
banklist = []
for b in Bank.objects.all():
bank_list.append(b)
#some other code here
context['bank_list'] = bank_list
return context
在我的模板中,我有一份建筑物清单和一份包含银行清单的下拉列表。 在下拉列表中,我想选择一个银行并仅显示那些客户拥有所选银行的建筑物。 作为初学者,我不知道如何进行这种动态查询。如何在我的view.py,urls.py和template.html中编写?