django用户配置文件数据库问题
我有一个名为 aaa 的应用程序,在 aaa 中的 models.py 中,我有这样的内容:
from django.db import models
from django.contrib.auth.models import User
class BBB(models.Model):
user = models.OneToOneField(User)
newsletter=models.BooleanField(default=False)
我添加到我的setting.py
AUTH_PROFILE_MODULE = 'aaa.BBB'
,然后我转到 django shell 并输入
>>> from django.contrib.auth.models import User
>>> a=User.objects.get(id=1)
>>> a.get_profile()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/contrib/auth/models.py", line 373, in get_profile
self._profile_cache = model._default_manager.using(self._state.db).get(user__id__exact=self.id)
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/db/models/query.py", line 347, in get
% self.model._meta.object_name)
DoesNotExist: BBB matching query does not exist.
Somebody知道出了什么问题? 编辑:我做manage.pysyncdb
I have app called aaa and in models.py in aaa i have sth like:
from django.db import models
from django.contrib.auth.models import User
class BBB(models.Model):
user = models.OneToOneField(User)
newsletter=models.BooleanField(default=False)
I add to my setting.py
AUTH_PROFILE_MODULE = 'aaa.BBB'
then i go to django shell and type
>>> from django.contrib.auth.models import User
>>> a=User.objects.get(id=1)
>>> a.get_profile()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/contrib/auth/models.py", line 373, in get_profile
self._profile_cache = model._default_manager.using(self._state.db).get(user__id__exact=self.id)
File "/usr/local/lib/python2.6/dist-packages/Django-1.2.5-py2.6.egg/django/db/models/query.py", line 347, in get
% self.model._meta.object_name)
DoesNotExist: BBB matching query does not exist.
Some body have idea what is wrong?
Edit: I do manage.py syncdb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://docs.djangoproject.com/ en/dev/topics/auth/#storing-additional-information-about-users
他们提到的信号并没有真正记录为 django 风格,他们提供了代码示例,因此我将为您创建一个示例:
http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
The signal they mention is not really documented django-style where they provide code examples, so I will create an example for you:
没关系,一切正常。
DoesNotExist:BBB 匹配查询不存在。
表示该用户没有 BBB(用户配置文件)(匹配查询,即获取该用户的用户配置文件)。使用
DoesNotExist
异常断言特定用户是否具有关联的用户配置文件。当您创建与用户a
相关的BBB
实例时,您不会收到DoesNotExist
异常。That's ok, everything works.
DoesNotExist: BBB matching query does not exist.
means there is no BBB (user profile) for this user (matching query, i.e. get me the user profile for this user).Use the
DoesNotExist
exception to assert whether a particular user has an associated user profile. When you create aBBB
instance which is related to usera
, you won't get aDoesNotExist
exception.