我需要加载与商店关联的酒的价格以及通用酒数据库中的其他数据。我知道如何加载数据,以及如何在两个不同的表中显示信息,但我需要该商店价格只是现有表中的另一行,我不知道该怎么做。这是视图:
def store(request, store_id=1):
a = Store.objects.get(StoreID=store_id)
b = StoreLiquor.objects.filter(storeID__exact=a).values_list('liquorID', flat=True)
x = StoreLiquor.objects.filter(storeID_exact=a)
args = {}
args['liquors'] = Liquor.objects.filter(id__in=b)
args['prices'] = x
args['a'] = a
return render(request, 'store.html', args)
这是html:
<pre>
<code>
{% if liquors.count > 0 %}
<table class="sortable">
<thead>
<tr>
<th scope="col">Liquor Code</th>
<th scope="col">Brand Name</th>
<th scope="col">Vendor Name</th>
</tr>
</thead>
<tbody>
{% for liquor in liquors %}
<tr>
<td>{{ liquor.LiquorCode }}</td>
<td><a href="/stores/storeliquors/{{ a.StoreID }}/{{ liquor.id }}/">{{ liquor.BrandName }}</a></td>
<td>{{ liquor.VendorName }}</td>
<td>{{ liquor.StorePrice }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>None to show!</p>
{% endif %}
</code>
</pre>
楷模:
class StoreLiquor(models.Model):
StoreLiquorID = models.AutoField(primary_key=True)
liquorID = models.ForeignKey(Liquor)
storeID = models.ForeignKey(Store)
StorePrice = models.DecimalField('Store Price', max_digits=5, decimal_places=2)
class Liquor(models.Model):
LiquorCode = models.PositiveSmallIntegerField('Liquor Code', max_length=5)
BrandName = models.CharField('Brand Name', max_length=32)
ADANumber = models.PositiveSmallIntegerField('ADA Number', max_length=3)
ADAName = models.CharField('ADA Name', max_length=25)
VendorName = models.CharField('Vendor Name', max_length=25)
LiquorType = models.CharField('ADA Name', max_length=20)
Proof = models.DecimalField(max_digits=3, decimal_places=1)
BottleSize = models.CharField('Bottle Size', max_length=7)
PackSize = models.PositiveSmallIntegerField('PackSize', max_length=3)
OnPremisePrice = models.DecimalField('On Premise Price', max_digits=5, decimal_places=2)
OffPremisePrice = models.DecimalField('Off Premise Price', max_digits=5, decimal_places=2)
ShelfPrice = models.DecimalField('Shelf Price', max_digits=5, decimal_places=2)
GlobalTradeItemNumber1 = models.BigIntegerField('Global Trade Item Number 1', max_length=14)
GlobalTradeItemNumber2 = models.BigIntegerField('Global Trade Item Number 2', max_length=14)
class Store(models.Model):
StoreID = models.AutoField(primary_key=True)