Django:检索抽象派生模型

发布于 2024-07-13 17:53:42 字数 2621 浏览 8 评论 0原文

在得到我的 上一个问题的良好答案后,我遇到了另一个问题。

我遵循了第三个方法,了解 djangodocs 关于抽象模型的说法子类化

我正在使用最新的 Django,版本 9814。我得到的奇怪行为:

In [1]: o = Order()

In [2]: o.save()
DEBUG:root:STORING EVENT MESSAGE: Order created. pk=2 -- LEVEL TOP
DEBUG:root:Saving order pk=2 

# it actually does exist in the database, so everything's cool.

In [3]: o.id
Out[3]: 2L # huh?

In [4]: o._default_manager
Out[4]: <django.db.models.manager.Manager object at 0x16e5370>

In [5]: Order.objects.all()
Out[5]: []

# WTF? and even more WTF below:

In [6]: Order.objects.get(id=2) 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/adam/_dev/cashfire/<ipython console> 

/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/manager.py in get(self, *args, **kwargs)
     91 
     92     def get(self, *args, **kwargs):
---> 93         return self.get_query_set().get(*args, **kwargs)
     94 
     95     def get_or_create(self, **kwargs):

/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py in get(self, *args, **kwargs)
    333         """
    334         clone = self.filter(*args, **kwargs)
--> 335         num = len(clone)
    336         if num == 1:
    337             return clone._result_cache[0]

/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py in __len__(self)
    159                 self._result_cache = list(self._iter)
    160             else:
--> 161                 self._result_cache = list(self.iterator())
    162         elif self._iter:
    163             self._result_cache.extend(list(self._iter))

/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py in iterator(self)
    286             else:
    287                 # omit aggregates in object creation
--> 288                 obj = self.model(*row[index_start:aggregate_start])
    289 
    290             for i, k in enumerate(extra_select):

TypeError: __init__() takes exactly 1 argument (5 given)

After getting fine answer to my previous question, I came across another problem.

I followed the third approach, being aware of what djangodocs say about abstract model subclassing.

I am using the latest Django, rev 9814. The strange behaviour I get:

In [1]: o = Order()

In [2]: o.save()
DEBUG:root:STORING EVENT MESSAGE: Order created. pk=2 -- LEVEL TOP
DEBUG:root:Saving order pk=2 

# it actually does exist in the database, so everything's cool.

In [3]: o.id
Out[3]: 2L # huh?

In [4]: o._default_manager
Out[4]: <django.db.models.manager.Manager object at 0x16e5370>

In [5]: Order.objects.all()
Out[5]: []

# WTF? and even more WTF below:

In [6]: Order.objects.get(id=2) 
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/adam/_dev/cashfire/<ipython console> 

/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/manager.py in get(self, *args, **kwargs)
     91 
     92     def get(self, *args, **kwargs):
---> 93         return self.get_query_set().get(*args, **kwargs)
     94 
     95     def get_or_create(self, **kwargs):

/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py in get(self, *args, **kwargs)
    333         """
    334         clone = self.filter(*args, **kwargs)
--> 335         num = len(clone)
    336         if num == 1:
    337             return clone._result_cache[0]

/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py in __len__(self)
    159                 self._result_cache = list(self._iter)
    160             else:
--> 161                 self._result_cache = list(self.iterator())
    162         elif self._iter:
    163             self._result_cache.extend(list(self._iter))

/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/django/db/models/query.py in iterator(self)
    286             else:
    287                 # omit aggregates in object creation
--> 288                 obj = self.model(*row[index_start:aggregate_start])
    289 
    290             for i, k in enumerate(extra_select):

TypeError: __init__() takes exactly 1 argument (5 given)

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

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

发布评论

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

评论(1

思念绕指尖 2024-07-20 17:53:42

好吧,我的错(务实程序员的“不要再责怪 SQL”)。
这正是回溯所说的,忘记了 __init__ 的 args & 。 夸格。 愚蠢的我。

Ok, my fault (the Pragmatic Progammer's "Don't blame SQL" again).
It was exactly what traceback was saying, forgot about __init__'s args & kwagrs. Silly me.

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