Select reverse Foreign Key in ModelChoicefield
<p>I have an order form where the user can choose one item and select a quantity. The price depends on how much is ordered. For example, each item is $10 if you order &lt;100, but $7 if you order 100-200.</p>

<p>In the template, I want to display the pricing information underneath the form for each choice.</p>

<p>These are my Models:</p>

<pre><code>class Product(models.Model):
    name = models.TextField()

class Price(models.Model):
    """
        This lets us define rules such as:
        When ordering &lt;100 items, the price is $10
        When ordering 100-200 items, the price is $7
        When ordering 200-300 items, the price is $5
        etc
    """
    price = models.FloatField()
    min_quantity = models.PositiveIntegerField()
    max_quantity = models.PositiveIntegerField()
    product = models.ForeignKey(Product)

class Order(models.Model):
    product = models.ForeignKey(Product, null=False, blank=False, default='')
    quantity = models.IntegerField()
</code></pre>

<p>I can loop over the form fields and the queryset independently:</p>

<pre><code>{% for choice in form.product.field.queryset %}
    &lt;h1&gt;{{choice}} {{choice.price_set.all}}&lt;/h1&gt;
{% endfor %}

{% for choice in form.product %}
    &lt;h1&gt;{{ choice.tag }} {{ choice.choice_label }}&lt;/h1&gt;
{% endfor %}
</code></pre>

<p>... but I don't know how to combine the loops to display the prices underneath the form fields.</p>

<p>Essentially, I want to select a reverse foreign key from a ModelChoicefield widget. I either need to loop over both the form fields and the queryset at once or access elements in the queryset from the form element. Ideally, this is what I'd like to do in my template:</p>

<pre><code>{% for choice in form.product %}
    &lt;h1&gt;{{ choice.tag }} {{ choice.choice_label }}&lt;/h1&gt;
    {% for price in choice.price_set.all %}
        &lt;h1&gt;{{price}} etc...&lt;/h1&gt;
    {% endfor %}
{% endfor %}
</code></pre>

<p>Surely I'm not the first person with this use case. What's the best way to do this?</p>

<p><strong>Edit:</strong> As requested, this is my form and my view. Reviewing this, I suppose I should have mentioned I was using the <code>RadioSelect</code> widget.</p>

<p>Form:</p>

<pre><code>class OrderForm(forms.ModelForm):
    class Meta:
        exclude = ['date_added']
        widgets = {
            'mailer': forms.RadioSelect
        }
        model = Order
</code></pre>

<p>View:</p>

<pre><code>def processOrder(request):
    if request.method == 'POST':
        orderForm = OrderForm(request.POST)
        if orderForm.is_valid():
            orderObject = orderForm.save()
            return render(request, TEMPLATE_PREFIX + "confirm.html", {"orderObject": orderObject})
        else:
            return render(request, TEMPLATE_PREFIX + "register.html", { "form": orderForm })
    else:
        return render(request, TEMPLATE_PREFIX + "register.html", { "form": OrderForm()})
</code></pre>

<p>For (non)perfectionists with deadlines, this code works, albeit inefficiently.</p>

<pre><code>{% for choice in form.product %}
    {% for price_candidate in form.mailer.field.queryset %}
        {% if price_candidate.id == choice.choice_value|add:0 %}
            &lt;h1&gt;{{ choice.tag }} {{ choice.choice_label }}&lt;/h1&gt;
            {% for price in price_candidate.price_set.all %}
               &lt;h1&gt;{{price}} etc...&lt;/h1&gt;
            {% endfor %}
        {% endif %}
    {% endfor %}
{% endfor %}
</code></pre>

<p>(The <code>add:0</code> hack converts choice_value into an int. cf <a href="http://zurb.com/forrst/posts/Compare_string_and_integer_in_Django_templates-0Az" rel="nofollow">http://zurb.com/forrst/posts/Compare_string_and_integer_in_Django_templates-0Az</a> )</p>

python django django-templates