0

这是 /askbot/models 文件夹中 __init__.py 文件的一部分:

def user_update_avatar_type(self):
"""counts number of custom avatars
and if zero, sets avatar_type to False,
True otherwise. The method is called only if
avatar application is installed.
Saves the object.
"""

if 'avatar' in django_settings.INSTALLED_APPS:
    if self.avatar_set.count() > 0:
        self.avatar_type = 'a'
    else:
        self.avatar_type = _check_gravatar(self.gravatar)
else:
        self.avatar_type = _check_gravatar(self.gravatar)
self.save()

如您所见,在某些时候它调用 self. avatar_set .count (),我认为它应该是来自 /Django/Contrib/auth 的用户类中的原生方法,或者像这个 __init__.py 文件中的许多其他方法一样由 User.add_to_class 方法添加

但我似乎无法找到这个 'avatar_set' 方法的定义位置。

谢谢

4

1 回答 1

0

这是一种反向关系,当你定义一个指向模型的 ForeignKey 时,Django 会自动添加这种关系。请参阅查询文档

于 2014-05-26T10:01:29.400 回答