django按对象实例过滤外键(TypeError)

时间:2015-10-12 09:14:48

标签: python django

class CommentKol(models.Model):
    ad_user = models.ForeignKey(UserProfile, related_name='comment_ad_aduser_set')
    kol_user = models.ForeignKey(UserProfile, related_name='comment_ad_koluser_set')
    kol_detail = models.ForeignKey(UserKolMap)

class UserKolMap(models.Model):
    user = models.ForeignKey(UserProfile)
    kol_id = models.CharField('kol userid', max_length=32)
    kol_type = models.CharField(max_length=16)

没关系:

kol_detail = get_object_or_404(UserKolMap, kol_id=kol_id, kol_type=kol_type)
comments = CommentKol.objects.filter(kol_detail=kol_detail.id)

但是错误:

kol_detail = get_object_or_404(UserKolMap, kol_id=kol_id, kol_type=kol_type)
comments = CommentKol.objects.filter(kol_detail=kol_detail)

我检查了kol_detail的值,发现kol_detail.userNone,但kol_detail.user_id不是None(它是12)。

回溯切换到复制粘贴视图:

Environment:


Request Method: GET
Request URL: http://localhost:8000/view_kol_credit/?kol_d=2697416452&kol_t=wb

Django Version: 1.8.1
Python Version: 2.7.9
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'kol',
 'webim',
 'notifications',
 'ueditor')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "D:/github/mx_kol\kol\decorators.py" in _dec
  9.             return view_func(request, *args, **kwargs)
File "D:/github/mx_kol\kol\views\views_ad.py" in view_kol_credit
  2204.     comments = CommentKol.objects.filter(kol_detail=kol_detail)
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in manager_method
  127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in filter
  679.         return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  697.             clone.query.add_q(Q(*args, **kwargs))
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
  1304.         clause, require_inner = self._add_q(where_part, self.used_aliases)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1331.                     current_negated=current_negated, connector=connector, allow_joins=allow_joins)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1150.         value, lookups, used_joins = self.prepare_lookup_value(value, lookups, can_reuse, allow_joins)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in prepare_lookup_value
  1000.             value = value.resolve_expression(self, reuse=can_reuse, allow_joins=allow_joins)

Exception Type: TypeError at /view_kol_credit/
Exception Value: 'NoneType' object is not callable

https://docs.djangoproject.com/en/1.8/topics/db/queries/#queries-over-related-objects

涉及相关对象的查询遵循与涉及正常值字段的查询相同的规则。指定要匹配的查询的值时,可以使用对象实例本身,也可以使用对象的主键值。

例如,如果您的博客对象b的id = 5,则以下三个查询将完全相同:

Entry.objects.filter(blog=b) # Query using object instance
Entry.objects.filter(blog=b.id) # Query using id from instance
Entry.objects.filter(blog=5) # Query using id directly

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

我发现kol_detail.userNone,因此我检查了UserKolMap的模型,发现有人添加了此代码:

def __getattr__(self, item):
    if item == "userid":
        return self.kol_id

然后将其修改为:

def __getattr__(self, item):
    if item == "userid":
        return self.kol_id

    return super(UserKolMap, self).__getattr__(self, item)

没关系。

我没有粘贴整个代码,这是我的错!