0

currently I'm trying to show part quantity (quan) together with part name in the dropdown. I have a Part table that carries the part name and part quantity and this table called as ForeignKey into the Order table. So, in the Order form during choose the part name from the part dropdown, I would like to show part quantity as well besides the part name. Any idea to make it like that?

models.py

class Part(models.Model):
    partno = models.CharField(max_length=50)
    partname = models.CharField(max_length=50)
    quan = models.PositiveIntegerField(default= 0)

    def __str__(self):
       return '{}, quantity - {}'.format(self.partname, self.quan)

class Order(models.Model):
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    part = models.ForeignKey(Part, on_delete=models.CASCADE)

views.py

def create_order(request):
    from django import forms
    form = OrderForm()

    if request.method == 'POST':
        for form_data in forms_data:
            forms = OrderForm(request.POST)
            if forms.is_valid():
                supplier = forms.cleaned_data['supplier']
                product = forms.cleaned_data['product']
                part = forms.cleaned_data['part']
 
                order = Order.objects.create(
                    supplier=supplier,
                    product=product,
                    part=part,
                )

        return redirect('order-list')
    context = {
        'form': form
    }
    return render(request, 'store/addOrder.html', context)

HTML

<form action="#" method="post" id="form-container" novalidate="novalidate">
   {% csrf_token %}

     <div class="form-group">
        <label for="product" class="control-label mb-1">Product</label>
        {{ form.product }}
     </div>

     <div class="form-group">
        <label for="supplier" class="control-label mb-1">Supplier</label>
        {{ form.supplier }}
     </div>
                                        
     <div class="form-group">
        <label for="part" class="control-label mb-1">Part Name</label>
        {{ form.part }}
     </div>
</form>
Toufiqur Rahman
  • 202
  • 3
  • 17

1 Answers1

1

You will have to write "__ str __"(without spaces between str and __) method for model 'Part'

def __str__(self):
    return '{}, quantity - {}'.format(self.partname, self.quan)

Check this post also: What is doing __str__ function in Django?

NickNaskida
  • 106
  • 7
  • Thanks for the suggestion, but in the form of order still I am not getting part quan along with partname. I have updated my model above as per your suggestions. – Toufiqur Rahman Jul 02 '21 at 09:50
  • here is forms.py https://gist.github.com/TaufiqurRahman45/bbd2286f258cc919bfbc962db8dad801 – Toufiqur Rahman Jul 02 '21 at 10:00
  • I you have to rewrite your code, with using django model forms and it will work. Here is the documentation: https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/ – NickNaskida Jul 02 '21 at 10:01
  • man you have only 2 Select widgets in PartForm is and you said you want dropdown on partname, so change partname's widget to Select – NickNaskida Jul 02 '21 at 10:11