views:

26

answers:

1

I'm trying to save a new object from a django model using a POST data querydict. This is part of a PISTON handler. I've seen this done in numerous examples, but I just can't get it to work.

Here is my code:

class FestHandler(BaseHandler):
    model = Deal

    def create(self, request):
        """
        Creates a new fest.
        """
        attrs = self.flatten_dict(request.POST)
        postcopy = request.POST.copy()
        if self.exists(**attrs):
            return rc.DUPLICATE_ENTRY
        else:
            loc = Location.objects.get(pk=attrs['location'])
            postcopy['location'] = loc
            fest = Fest(postcopy)
            fest.save()
            return fest

Here is the error I receive every time:
Exception was: int() argument must be a string or a number, not 'QueryDict'

I realize what the error means, so basically I am asking how I can save a new "Fest" by passing in the whole POST dictionary without having to type in the keys manually every time like this:

loc = Location.objects.get(pk=attrs['location'])
fest = Fest(
    location=loc,
    name=attrs['name'],
    description=attrs['description'],
    details=attrs['details'],
)

Thanks for your help!

+1  A: 

First, I think you'll be happier of you explicitly make your PK's into integers.

        loc = Location.objects.get(pk=int(attrs['location']))

Second, you should use a Form.

  1. It validates the fields for you.

  2. It will create the Model object from the Form object.

Read this. http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/

S.Lott
Hmm. That seemed to work. However, now it returns an HTML form after I save it, instead of the Fest object that PISTON renders to JSON. Is there an easy way to change this? Or, is there a way to do this without using a "Form"? Thanks!
Sunsu
Nevermind, I got it. Sorry for my stupid questions, I'm still not fully comfortable with Django OR python. Thanks for your help!
Sunsu