Issue
views.py -
if product.variant != "None":
variants = Variants.objects.filter(product_id=product.id)
colors = Variants.objects.filter(product_id=product.id, size_id=variants[0].size_id)
sizes = Variants.objects.raw("SELECT * FROM core_variants WHERE product_id=%s GROUP BY size_id",[id])
variant = Variants.objects.get(id=variants[0].id)
context = {'sizes': sizes,
'colors': colors,
'variant': variant,
}
return render(request, 'core/product-details.html', context)
product-details.html -
<select name="size" id="size" class="form-control">
{% for rs in sizes %}
<option {% if variant.size_id == rs.size_id %}selected{% endif %} value="{{rs.size_id}}">{{rs.size_id.title}}</option>
{% endfor %}
</select>
The error i'm getting is "Error binding parameter 1: type 'builtin_function_or_method' is not supported"
The line i'm getting error is "{% for rs in sizes %}"
I just want to show all the options from the queryset
Solution
The problem is that you use [id]
, but id(…)
[python-doc] is a builtin function to get the "identity" of an object. You thus can use product.id
:
if product.variant is not None:
variants = Variants.objects.filter(product_id=product.id)
colors = Variants.objects.filter(
product_id=product.id, size_id=variants[0].size_id
)
sizes = Variants.objects.raw(
'SELECT * FROM core_variants WHERE product_id=%s GROUP BY size_id',
[product.id],
)
variant = Variants.objects.get(id=variants[0].id)
context = {
'sizes': sizes,
'colors': colors,
'variant': variant,
}
return render(request, 'core/product-details.html', context)
But it makes not much sense to write raw queries for this, nor to make two queries to get the Variant
s.
Answered By - willeM_ Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.