我想访问模板中的transaction_ambassador
。因此,我的方法是使用已经使用过的订单模型,它是foreign_key。我所做的是
{{ order.ambassador_transaction }}
但是我得到的只是ambassadors.AmbassadorTransaction.None
。我检查了几次,但它是通过foreign_key
连接的。有人知道我在做什么错吗?
views.py
class OrderListView(ListView):
allow_empty = False
template_name = 'orders/order_list.html'
def get_queryset(self):
return Attendee.objects.filter(
order__order_reference=self.kwargs['order_reference'],
order__access_key=self.kwargs['access_key'],
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['order'] = Order.objects.get(
order_reference=self.kwargs['order_reference'],
)
return context
models.py
class AmbassadorTransaction(models.Model):
order = models.ForeignKey(
Order,
on_delete=models.PROTECT,
related_name='ambassador_transaction'
)
ambassador = models.ForeignKey(
AmbassadorProfile,
on_delete=models.PROTECT,
related_name='ambassador_profile'
)
class Order(models.Model):
event = models.ForeignKey(
Event,
on_delete=models.PROTECT,
related_name='orders'
)
order_reference = models.CharField(
max_length=10,
unique=True,
)
total_gross = models.DecimalField(
max_digits=25,
decimal_places=2
)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
答案 0 :(得分:0)
您的Foo<D> fooD = fooAtoBtoCtoD
.through(fooA.composing())
.through(fooB.composing())
.through(fooC.composing());
模型中没有ambassador_transaction
外键。
如果您要访问与Order
的实例相关的所有AmbassadorTransaction
,则可以在视图中的Order
或模板的order.ambassador_transaction_set.all()
上进行迭代。
您还可以使用order.ambassador_transaction.all
从AmbassadorTransaction
对象访问订单
答案 1 :(得分:0)
由于您的models.ForeignKey(...)
位于AmbassadorTransaction
中,而不位于Order
中,因此这是一种解决方案:
{% for ambassador in order.ambassador_transaction.all %}
...
{% endfor %}