我如何在模板上的新字段中发送模型数据

时间:2017-02-14 02:38:10

标签: python django

  

我如何为我的模型制作自定义字段并将其发送给tempalte?

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

...

{% 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>

{{ i.dish.supp }}

为空

2 个答案:

答案 0 :(得分:0)

假设您粘贴了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

另外,请勿将eval()用于id_in - 这不是必需的。请注意,在我添加的代码中,只有一个i_dish_supp。看起来每个订单都需要一个订单,但我很难理解您要完成的任务。

你应该看看ccbv.co.uk它提供了一种很好的方式来查看基于类的视图及其所有方法。

答案 1 :(得分:0)

接下来,在我的模型中

@property
def get_supplements(self):
    return Supplement.objects.filter(id__in=eval(self.supplements))
模板中的

       {% for supp in i.get_supplements %}
           <span>{{ supp.name }}</span>
      {% endfor %}