How to implement a form to update the quantity?

366 views
Skip to first unread message

enemybass

unread,
Oct 1, 2012, 11:46:50 AM10/1/12
to django...@googlegroups.com
Hi. How to implement a form to update the quantity Car? I have no idea how to do it. Thanks

Code: http://dpaste.com/808558/

or here:

    class Car(models.Model):
        name = models.CharField(max_length=50)
        price = models.DecimalField()   
   
    class GarageCar(models.Model):
        car = models.ForeignKey('Car')
        garage = models.ForeignKey('Garage')
        quantity = models.IntegerField()
   
    class Garage(models.Model):
        name = models.CharField("Garage_Name", max_length=30)
        cars = models.ManyToManyField('Car', through='GarageCar', blank=True, null=True)
        owner = models.ForeignKey(User, related_name='owner_garage', verbose_name='Owner Garage')

In this way updates the quantity:

    In [2]: c = Car.objects.get(id=1) #here I need car ID
   
    In [3]: g = Garage.objects.get(owner=1)
   
    In [4]: q = 33 #here I need data from form
   
    In [5]: c= GarageCar.objects.filter(car=c, garage=g).update(quantity=q)



views:

    def show(request):
        user = request.user
        garage = Garage.objects.get(owner=user)
       
        return render_to_response('garage.html', {'garage': garage, 'form':form})
   
   
in garage.html:
   
    {% for item in garage.garagecar_set.all %}
            Car:      {{item.car.name}}
            Price:    {{item.car.price}}
            Quantity: {{item.quantity}}
            ID:       {{item.car.id}}
    {% endfor %}


in forms.py:

    from django import forms
   
    class QuantityForm(forms.Form):
        q = forms.IntegerField

How to create ORM query to update quantity?

How to display a form field next to each car in template (with button "Update")?

Anton Baklanov

unread,
Oct 1, 2012, 2:52:01 PM10/1/12
to django...@googlegroups.com
Hi!

You can use something like:

{% for item in garage.garagecar_set.all %}
  Car: {{item.car.name}} Price: {{item.car.price}} Quantity: {{item.quantity}} ID: {{item.car.id
}}
<form action="../update_q/{{item.pk}}/" method="post"/>
{{form.q}}
<input type="submit" value="update"/>
</form>
 {% endfor %}

and view like

def update_q(request, garagecar_id):
# get garagecar by id here, create form from request, set new value, save()


it to submit forms with ajax, to not reload entire page every time.


P.S.

it looks you are implementing Many-to-Many-Through relation. Django has support for it out of the box
https://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships



--
Regards,
Anton Baklanov

Anton Baklanov

unread,
Oct 1, 2012, 2:55:15 PM10/1/12
to django...@googlegroups.com
it is good idea to submit...

missed some words, lol


On Mon, Oct 1, 2012 at 9:52 PM, Anton Baklanov <antonb...@gmail.com> wrote:
it to submit forms with ajax, to not reload entire page every time.



--
Regards,
Anton Baklanov

Domi nika

unread,
Oct 10, 2012, 7:27:02 AM10/10/12
to django...@googlegroups.com
Thanks. Is it possible to display multiple form in loop?

2012/10/1 Anton Baklanov <antonb...@gmail.com>
--
Regards,
Anton Baklanov

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages