Django-Piston - 我无法在带有外键的模型上发布

发布于 2024-09-28 14:57:25 字数 438 浏览 0 评论 0原文

我正在尝试在我的 Django 项目上设置活塞。当我尝试在包含外键:位置的模型上发布(创建)新条目时,我遇到了困难。

这是我收到的确切错误:

无法分配“u'1'”:“Fest.location”必须是“Location”实例。

在上面的示例中,我尝试在 POST 中通过 location=1 发送。

我在这里做错了什么?当然,CREATE 支持外键...

更新:
需要明确的是,我使用 PISTON 来处理这些 REST API 请求。我的处理程序当前如下所示:

class FestHandler(BaseHandler):
    model = Fest`  

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`  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2024-10-05 14:57:25

您需要分配一个实际的对象。像下面这样的东西应该可以工作:

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

其中 obj 是您尝试保存的模型,其位置作为外键。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文