Monday, October 1, 2012

Newbie question - setting a model form foriegn key

Sorry if this is a dumb question, just picking up Django.

I've set up a payments form, to create payments associated with a contact (see payments model) however I'm having trouble figuring out how to save the payment with the relevant contact associated, as the contact isnt selected as part of the form (its in the URL instead).  I've included both the payment view and the view for processing it, my current approach results in a validation error. Anyone able to help?

Thanks

Keir

class Payment(models.Model):
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    date = models.DateField()
    contact = models.ForeignKey(Contact)

def payments_view(request, contact_id):
    contact = get_object_or_404(Contact, pk=contact_id)
    payments = Payment.objects.filter(contact=contact_id)
    rctx = RequestContext(request,{
        'contact': contact, 'payments': payments, 'form' : PaymentForm()
    })
    return render_to_response('CRMSite/contact_payments.html',rctx)

def save_payment(request,contact_id):
        PaymentForm(request.POST,contact=contact_id).save()
        return payments_view(request, contact_id)

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

No comments:

Post a Comment