cartridge list all products

22 views
Skip to first unread message

ek ekke

unread,
May 28, 2021, 4:56:21 AMMay 28
to Mezzanine Users
I am trying to list all products from cartridge, is that function all ready in there and how can i call it from a template?
Don't want to write the view if it's all ready in there somewhere.

Ken Bolton

unread,
May 28, 2021, 11:58:17 AMMay 28
to mezzanine-users
On Fri, May 28, 2021 at 4:56 AM ek ekke <ekk...@gmail.com> wrote:
I am trying to list all products from cartridge, is that function all ready in there and how can i call it from a template?
Don't want to write the view if it's all ready in there somewhere.

Check out the `Category` module, which is a subclass of Mezzanine's `Page`.

Danny

unread,
May 28, 2021, 7:16:18 PMMay 28
to mezzani...@googlegroups.com

Simplest way is

Product.objects.all()

To get that in a template, you might need to write a template tag, e.g. I added something to my Orders Change List template so that I could filter orders by a specific product (and export the results to CSV). The basics are:

In admin.py (or forms.py):

def get_all_products():
    return [(p.id, p.title) for p in Product.objects.all().order_by('title')]

class OrderByProductForm(forms.Form):
    product = forms.ChoiceField(label="Export filtered orders containing",
                                choices=[])

    def __init__(self, *args, **kwargs):
        super(OrderByProductForm, self).__init__(*args, **kwargs)
        self.fields["product"].widget = forms.Select(choices=get_all_products())

templatetags/myapp_tags.py:

@register.simple_tag
def export_order_by_product_form(request):
    querystring = request.GET.dict()
    return OrderByProductForm(queries=querystring)

And then in templates/admin/order_change_list.html:

{% extends "admin/change_list.html" %}
{% load admin_urls myapp_tags %}

{% block object-tools-items %}

<li><form action="/admin/order_export/" method="get">
   {% export_order_by_product_form request %}
   <input type="submit" value="Export to CSV" />
</form></li>
{{ block.super }}
{% endblock %}

Note then i have also defined a view for "order_export" which uses the selected Form item to find the right orders and export them to CSV.

Hope this helps.

Seeya. Danny.


ek ekke

unread,
May 31, 2021, 7:08:51 AMMay 31
to Mezzanine Users
Oh wow thanks guys
Reply all
Reply to author
Forward
0 new messages