Django 中的 get_or_create 通用关系 & python调试一般

发布于 2024-08-28 13:06:15 字数 1851 浏览 15 评论 0原文

我运行代码来从该演示中创建一般相关的对象: http://www.djangoproject.com/documentation/models/generic_relations/

一切都是最初很好:

>>> bacon.tags.create(tag="fatty")
<TaggedItem: fatty>
>>> tag, newtag = bacon.tags.get_or_create(tag="fatty")
>>> tag
<TaggedItem: fatty>
>>> newtag
False

但是我对我的应用程序感兴趣的用例:

>>> tag, newtag = bacon.tags.get_or_create(tag="wholesome")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 123, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 343, in get_or_create
    raise e
IntegrityError: app_taggeditem.content_type_id may not be NULL

在查看其他代码后我尝试了一些随机的事情:

>>> tag, newtag = bacon.tags.get_or_create(tag="wholesome", content_type=TaggedItem)
ValueError: Cannot assign "<class 'generics.app.models.TaggedItem'>": "TaggedItem.content_type" must be a "ContentType" instance.

或:

>>> tag, newtag = bacon.tags.get_or_create(tag="wholesome", content_type=TaggedItem.content_type)
InterfaceError: Error binding parameter 3 - probably unsupported type.

等。

我确信有人可以给我正确的语法,但真正的语法这里的问题是我不知道发生了什么。我使用强类型语言(x86 汇编、C++ 和 C#)进行开发已有十多年,但对 Python 还很陌生。我发现当这样的事情发生时,很难跟踪 Python 中发生的事情。

在我之前提到的语言中,解决这样的问题相当简单——检查方法签名并检查参数。看了半个小时的 Django 文档让我同样迷失。查看 get_or_create(self, **kwargs) 的源代码也没有帮助,因为没有方法签名并且代码看起来非常通用。下一步是调试该方法并尝试弄清楚发生了什么,但这似乎有点极端......

我似乎在这里缺少一些基本的操作原理......它是什么?以后遇到这样的问题我该如何自己解决?

I ran the code to create the generically related objects from this demo:
http://www.djangoproject.com/documentation/models/generic_relations/

Everything is good intially:

>>> bacon.tags.create(tag="fatty")
<TaggedItem: fatty>
>>> tag, newtag = bacon.tags.get_or_create(tag="fatty")
>>> tag
<TaggedItem: fatty>
>>> newtag
False

But then the use case that I'm interested in for my app:

>>> tag, newtag = bacon.tags.get_or_create(tag="wholesome")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 123, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 343, in get_or_create
    raise e
IntegrityError: app_taggeditem.content_type_id may not be NULL

I tried a bunch of random things after looking at other code:

>>> tag, newtag = bacon.tags.get_or_create(tag="wholesome", content_type=TaggedItem)
ValueError: Cannot assign "<class 'generics.app.models.TaggedItem'>": "TaggedItem.content_type" must be a "ContentType" instance.

or:

>>> tag, newtag = bacon.tags.get_or_create(tag="wholesome", content_type=TaggedItem.content_type)
InterfaceError: Error binding parameter 3 - probably unsupported type.

etc.

I'm sure somebody can give me the correct syntax, but the real problem here is that I have no idea what is going on. I have developed in strongly typed languages for over ten years (x86 assembly, C++ and C#) but am new to Python. I find it really difficult to follow what is going on in Python when things like this break.

In the languages I mentioned previously it's fairly straightforward to figure things like this out -- check the method signature and check your parameters. Looking at the Django documentation for half an hour left me just as lost. Looking at the source for get_or_create(self, **kwargs) didn't help either since there is no method signature and the code appears very generic. A next step would be to debug the method and try to figure out what is happening, but this seems a bit extreme...

I seem to be missing some fundamental operating principle here... what is it? How do I resolve issues like this on my own in the future?

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

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

发布评论

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

评论(2

掩饰不了的爱 2024-09-04 13:06:15

ContentType.objects .get_for_model() 将为您提供模型的适当 ContentType。将返回的对象作为 content_type 传递。

当谈到 Django 时,不要太担心“得到它”。 Django 一开始就很疯狂,鼓励实验和大量阅读文档和源代码。

ContentType.objects.get_for_model() will give you the appropriate ContentType for a model. Pass the returned object as content_type.

And don't worry too much about "getting it" when it comes to Django. Django is mostly insane to begin with, and experimentation and heavy reading of both documentation and source is encouraged.

剩余の解释 2024-09-04 13:06:15

我收集了一些 Django 调试链接。 其中最好的两个是Simon Willison 的帖子(具体来说,pdb 可能会让您在 Python(来自 C#/ VisualStudio 背景)和 Django 调试工具栏

I've collected some Django debugging links here. The two best out of the group are Simon Willison's post (specifically, pdb might make you feel more at home in Python, coming from a C#/ VisualStudio background) and the Django debug toolbar.

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