speckis
speckis

Reputation: 67

django insert table with form inputs manually

I need to have form input data as values sent to my table field. Problem is I can't do that the usual way, because the form is tied to another model. But I also need to populate this model with same form.

payment = SitePayment.objects.create(
                    user=request.user,
                    charge=charge.id,
                    site=psite,
                    # address=form.address,
                    # zipcode=form.zipcode,
                    # city=form.city,
                    # country=form.country,
                )

This was to me the simpler and logical way to go about it, but it doesn't work. Is there a way to have form inputs inserted into this model when manually inserting a table?

I can provide more code, but I thought it would be really messy for you to understand, so I'm trying this way.

Upvotes: 0

Views: 345

Answers (1)

speckis
speckis

Reputation: 67

I found the solution, which was incredibly simple and I now feel incredibly stupid. I'm new to django/python, though, so please no anger.

payment = SitePayment.objects.create(
                    user=request.user,
                    charge=charge.id,
                    site=psite,
                    address=form.cleaned_data['address'],
                    zipcode=form.cleaned_data['zipcode'],
                    city=form.cleaned_data['city'],
                    country=form.cleaned_data['country'],
                )

It should of course be "form.cleaned_data" and not just "form."

Upvotes: 2

Related Questions