How i can do custom fields for my Model and send it to tempalte?
我如何为我的模型做自定义字段并将其发送到tempalte?
supp - new property for Dish model. If i do print (i.dish.supp)
i will see Supplement objects But in template i don't have it... how i can get it in template.
supp - Dish模型的新属性。如果我打印(i.dish.supp)我将看到补充对象但在模板中我没有它...我怎么能在模板中得到它。
def get_context_data(self, **kwargs):
context = super(CartView, self).get_context_data(**kwargs)
user = self.request.user
try:
if user.is_authenticated():
userorders = UserOrder.objects.filter(user_id=user.id,
is_closed=0)
else:
user_hash = self.request.session.get('user_hash')
userorders = UserOrder.objects.filter(user_hash=user_hash,
is_closed=0)
except ObjectDoesNotExist:
context['data_dish'] = []
return context
try:
context['address'] = UserAddress.objects.get(
user_id=user.id,
is_main=1)
except ObjectDoesNotExist:
context['address'] = False
for order in userorders:
summ = order.order_summ = order.userorder.filter(
order_id=order.id).aggregate(Sum('price'))
for i in order.userorder.all():
sum = Supplement.objects.filter(
id__in=eval(i.supplements)).aggregate(
s=Sum('price'))
i.dish.supp = Supplement.objects.filter(
id__in=eval(i.supplements)).values('name')
context['orders'] = userorders
return context
in tempklate
在tempklate
......
......
{% for order in orders.all %}
<div class="main_order_{{ order.shop.id }} cart" >
<div class="cart-title">
<a href="/shop/{{ order.shop.id }}/">{{ order.shop.name }}</a>
{% for shop, msg in shop_msg %}{% if shop == order.shop %}{{ msg|safe|escape }}{% endif %}{% endfor %}
</div>
{% for i in order.userorder.all %}
<div class="cart-item">
<div class="col-xs-6 col-sm-3">
<img src="{% static 'img/roll.png' %}" alt="" width="90">
<div class="info">
<h5>{{ i.dish.name }}</h5>
<span>Салат, {{ i.dish.weight }} г.</span>
</div>
</div>
but
但
{{ i.dish.supp }}
is empty
是空的
2 个解决方案
#1
0
Assuming you pasted all of the code from your get_context_data()
method, the issue is that you aren't returning the context (look at the very last line), nor are you adding i.dish.supp
to the context (be careful with those periods, I changed them to underscores here):
假设您粘贴了get_context_data()方法中的所有代码,问题是您没有返回上下文(查看最后一行),也没有将i.dish.supp添加到上下文中(请注意那些时期,我把它们改成了下划线):
def get_context_data(self, **kwargs):
context = super(CartView, self).get_context_data(**kwargs)
user = self.request.user
try:
if user.is_authenticated():
userorders = UserOrder.objects.filter(user_id=user.id,
is_closed=0)
else:
user_hash = self.request.session.get('user_hash')
userorders = UserOrder.objects.filter(user_hash=user_hash,
is_closed=0)
except ObjectDoesNotExist:
context['data_dish'] = []
return context
try:
context['address'] = UserAddress.objects.get(
user_id=user.id,
is_main=1)
except ObjectDoesNotExist:
context['address'] = False
for order in userorders:
summ = order.order_summ = order.userorder.filter(
order_id=order.id).aggregate(Sum('price'))
for i in order.userorder.all():
sum = Supplement.objects.filter(
id__in=i.supplements).aggregate(
s=Sum('price'))
i.dish.supp = Supplement.objects.filter(
id__in=i.supplements).values('name')
context['i_dish_supp'] = i.dish.supp
context['orders'] = userorders
# And now we provide the context
return context
Also, don't use eval()
for id_in - it's not necessary. Keep in mind that in the code I added, there will only be one i_dish_supp
. It looks like you want one for each order, but I'm having a hard time understanding what you're trying to accomplish.
另外,不要对id_in使用eval() - 这不是必需的。请记住,在我添加的代码中,只有一个i_dish_supp。看起来你想要每个订单一个,但我很难理解你想要完成的任务。
You should take a look at ccbv.co.uk it provides a great way to see the class based views, and all of their methods.
你应该看一下ccbv.co.uk,它提供了一种很好的方式来查看基于类的视图及其所有方法。
#2
0
i do next, in my model
我在我的模型中做下一步
@property
def get_supplements(self):
return Supplement.objects.filter(id__in=eval(self.supplements))
in template
在模板中
{% for supp in i.get_supplements %}
<span>{{ supp.name }}</span>
{% endfor %}
#1
0
Assuming you pasted all of the code from your get_context_data()
method, the issue is that you aren't returning the context (look at the very last line), nor are you adding i.dish.supp
to the context (be careful with those periods, I changed them to underscores here):
假设您粘贴了get_context_data()方法中的所有代码,问题是您没有返回上下文(查看最后一行),也没有将i.dish.supp添加到上下文中(请注意那些时期,我把它们改成了下划线):
def get_context_data(self, **kwargs):
context = super(CartView, self).get_context_data(**kwargs)
user = self.request.user
try:
if user.is_authenticated():
userorders = UserOrder.objects.filter(user_id=user.id,
is_closed=0)
else:
user_hash = self.request.session.get('user_hash')
userorders = UserOrder.objects.filter(user_hash=user_hash,
is_closed=0)
except ObjectDoesNotExist:
context['data_dish'] = []
return context
try:
context['address'] = UserAddress.objects.get(
user_id=user.id,
is_main=1)
except ObjectDoesNotExist:
context['address'] = False
for order in userorders:
summ = order.order_summ = order.userorder.filter(
order_id=order.id).aggregate(Sum('price'))
for i in order.userorder.all():
sum = Supplement.objects.filter(
id__in=i.supplements).aggregate(
s=Sum('price'))
i.dish.supp = Supplement.objects.filter(
id__in=i.supplements).values('name')
context['i_dish_supp'] = i.dish.supp
context['orders'] = userorders
# And now we provide the context
return context
Also, don't use eval()
for id_in - it's not necessary. Keep in mind that in the code I added, there will only be one i_dish_supp
. It looks like you want one for each order, but I'm having a hard time understanding what you're trying to accomplish.
另外,不要对id_in使用eval() - 这不是必需的。请记住,在我添加的代码中,只有一个i_dish_supp。看起来你想要每个订单一个,但我很难理解你想要完成的任务。
You should take a look at ccbv.co.uk it provides a great way to see the class based views, and all of their methods.
你应该看一下ccbv.co.uk,它提供了一种很好的方式来查看基于类的视图及其所有方法。
#2
0
i do next, in my model
我在我的模型中做下一步
@property
def get_supplements(self):
return Supplement.objects.filter(id__in=eval(self.supplements))
in template
在模板中
{% for supp in i.get_supplements %}
<span>{{ supp.name }}</span>
{% endfor %}