Lyle Pratt
Lyle Pratt

Reputation: 5956

Django-Piston - I Can't POST on a model with a ForeignKey

I'm trying to set up piston on my Django project. I ran into a brick wall when I tried to POST (create) a new entry on a model that contains a ForeignKey: location.

Here is the exact error I receive:

Cannot assign "u'1'": "Fest.location" must be a "Location" instance.

In the above example, I tried to send over location=1 in the POST.

What am I doing wrong here? Surely Foreign Keys are supported on CREATEs...

Update:
To be clear, I'm using PISTON to handle these REST API requests. My Handler currently looks like this:

class FestHandler(BaseHandler):
    model = Fest`  

Upvotes: 1

Views: 754

Answers (1)

ars
ars

Reputation: 123518

You need to assign an actual object. Something like the following should work:

loc = Location.objects.get(pk=1)
obj.location = loc
obj.save()

where obj is the model you're trying to save which has location as a foreign key.

Upvotes: 2

Related Questions