开发者

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

I'm trying t开发者_C百科o 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`  


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜