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.
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())
@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.