Ole*_*nko 0 django django-models django-managers
我有以下型号:
class UserProfile(models.Model):
"""
User profile model, cintains a Foreign Key, which links it to the
user profile.
"""
about = models.TextField(blank=True)
user = models.ForeignKey(User, unique=True)
ranking = models.IntegerField(default = 1)
avatar = models.ImageField(upload_to="usermedia", default = 'images/js.jpg')
updated = models.DateTimeField(auto_now=True, default=datetime.now())
is_bot = models.BooleanField(default = False)
is_active = models.BooleanField(default = True)
is_free = models.BooleanField(default = True)
objects = ProfileManager()
def __unicode__(self):
return u"%s profile" %self.user
Run Code Online (Sandbox Code Playgroud)
还有经理
class ProfileManager(models.Manager):
"""
Stores some additional helpers, which to get some profile data
"""
def get_active_members(self):
'''
Get all people who are active
'''
return self.filter(is_active = True)
Run Code Online (Sandbox Code Playgroud)
什么时候,我尝试调用UserProfile.obgets.get_active_members()之类的东西
我得到了
raise AttributeError, "Manager isn't accessible via %s instances" % type.__name__
Run Code Online (Sandbox Code Playgroud)
AttributeError:无法通过UserProfile实例访问Manager
能否请你帮忙
管理器仅适用于模型类,不适用于模型实例.
这将有效:
UserProfile.objects
Run Code Online (Sandbox Code Playgroud)
这不会:
profile = UserProfile.objects.get(pk=1)
profile.objects
Run Code Online (Sandbox Code Playgroud)
换句话说,如果您是在调用它实例的UserProfile,它会提高你看例外.您可以确认一下如何访问经理吗?
来自文档:
管理员只能通过模型类而不是模型实例访问,以强制实现"表级"操作和"记录级"操作之间的分离
| 归档时间: |
|
| 查看次数: |
1696 次 |
| 最近记录: |