django用户配置文件数据库问题

发布于 2024-10-21 12:06:59 字数 1093 浏览 5 评论 0原文

我有一个名为 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 技术交流群。

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

发布评论

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

评论(2

我恋#小黄人 2024-10-28 12:06:59

http://docs.djangoproject.com/ en/dev/topics/auth/#storing-additional-information-about-users

方法 get_profile() 不
创建配置文件(如果没有)
存在。您需要注册一个处理程序
对于信号
django.db.models.signals.post_save 上
用户模型,并且在处理程序中,
如果created=True,则创建关联的
用户个人资料。

他们提到的信号并没有真正记录为 django 风格,他们提供了代码示例,因此我将为您创建一个示例:

from django.db.models import signals
from django.contrib.auth.models import User

def create_userprofile(sender, **kwargs):
    created = kwargs['created'] # object created or just saved?

    if created:
        BBB.objects.create(user=kwargs['instance'])  # instance is the user
        # create a BBB "profile" for your user upon creation.
        # now every time a user is created, a BBB profile will exist too.
        # user.BBB or user.get_profile() will always return something

signals.post_save.connect(create_userprofile, sender=User)

http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

The method get_profile() does not
create the profile, if it does not
exist. You need to register a handler
for the signal
django.db.models.signals.post_save on
the User model, and, in the handler,
if created=True, create the associated
user profile.

The signal they mention is not really documented django-style where they provide code examples, so I will create an example for you:

from django.db.models import signals
from django.contrib.auth.models import User

def create_userprofile(sender, **kwargs):
    created = kwargs['created'] # object created or just saved?

    if created:
        BBB.objects.create(user=kwargs['instance'])  # instance is the user
        # create a BBB "profile" for your user upon creation.
        # now every time a user is created, a BBB profile will exist too.
        # user.BBB or user.get_profile() will always return something

signals.post_save.connect(create_userprofile, sender=User)
只是在用心讲痛 2024-10-28 12:06:59

没关系,一切正常。 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 a BBB instance which is related to user a, you won't get a DoesNotExist exception.

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