【问题标题】:Django, Error: UserProfile matching query does not existDjango,错误:用户配置文件匹配查询不存在
【发布时间】:2012-02-19 01:25:29
【问题描述】:

我正在学习 django。现在,我需要创建一个用户配置文件。

我已经创建了模型

class UserProfile(models.Model):  
user = models.OneToOneField(User)  
active = models.BooleanField()
address = models.CharField('Shipping address', max_length=150, blank=True, null=True)
telephone = models.CharField('Telephone number for shipping', max_length=20, blank=True, null=True)
steps = models.DecimalField('Steps since creation', max_digits=100, decimal_places=2, null=True)
active = models.BooleanField()

def __str__(self):  
    return "%s's profile" % self.user  

在一个名为accounting的应用程序中。我已经创建了

def create_user_profile(sender, **kwargs):
#When creating a new user, make a profile for him or her.
u = kwargs["instance"]
if not UserProfile.objects.filter(user=u):
    UserProfile(user=u).save()

post_save.connect(create_user_profile, sender=User)

因此,每次创建用户时,都会自动创建一个配置文件。我已经创建并验证了用户是在 userprofile 表中创建的。我也去了壳。我寻找那个 ID 为 4 的用户。我打印了用户 4 的地址并得到了地址。所以我确信它们是相互联系的并且可以工作的。但是当我转到 HTML 时,我得到了错误。

这里是视图。

    from accounting.models import UserProfile, Charge, Wallet
from django.shortcuts import get_object_or_404, RequestContext
from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template import Context, loader
from django.contrib.auth.forms import UserCreationForm

#def userprofile(request, user_id):
def userprofile(request, user_id):


user_profile = request.user.get_profile()
active = user_profile.active
return render_to_response('accounting/templates/userprofile.html', {
    'user_profile': user_profile,   
    'active': active,
}, context_instance=RequestContext(request))

谢谢。

【问题讨论】:

    标签: django


    【解决方案1】:

    确保你在 settings.py 中设置了AUTH_PROFILE_MODULE = 'my_profile_app.UserProfile'

    【讨论】:

    • 这不是答案或解决方案吗?
    • 是的,我不得不提一下。我在设置 AUTH_PROFILE_MODULE = 'accounting.UserProfile' 中有这个
    • 洛特,请帮帮我。你知道为什么它不工作吗?谢谢。
    • @S.Lott 很公平,我认为你对内容的评论比措辞更多
    【解决方案2】:

    代替:

    request.user.get_profile()
    

    用途:

    request.user.userprofile
    

    经过多年的 Django 开发,从不需要 AUTH_PROFILE_MODULE 或 get_profile()。我不知道使用 get_profile() (如果有的话)有什么好处,但这似乎是不必要的麻烦。

    实际上,我使用 django-annoying 的 AutoOneToOneField 的麻烦更少:https://bitbucket.org/offline/django-annoying/wiki/Home

    更多关于OneToOne:https://docs.djangoproject.com/en/dev/topics/db/models/#one-to-one-relationships

    【讨论】:

    • 感谢您的回答,但它不起作用。它给了我同样的错误。 UserProfile 匹配查询不存在。他们都有一个配置文件,从 2 到 4。我正在尝试访问数字 2、3 和 4,它给了我同样的错误。
    • 好的,我找到了解决方案,但是没有像你告诉我的那样工作,我使用的是 user = User.objects.get(pk=user_id) 而不是 request.user.get_profile() 并且它有效.所以现在我可以去 Userprofile 并获取所有信息。但是为什么没有像每个人那样工作呢?
    • 感谢pastylegs 提供详细信息。我现在可以看到 get_profile() 的优势,它非常聪明。 "user = User.objects.get(pk=user_id) " 我很确定它是可以改进的——除非你从请求或外部的东西中得到 user_id。
    • 看了get_profile()的代码后,没有发现任何缓存优于request.user.someOneToOneRelation。它只是缓存在一个实例变量中,我假设 django 默认为 OneToOne 执行此操作(例如,调用 request.user.someOneToOneRelation 两次不会导致 2 个查询吗?)。所以我现在迷路了,有用还是没用?
    • 我认为这就是为什么 Django 建议使用 user = models.ForeignKey(User, null=False)
    猜你喜欢
    • 2013-12-05
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2015-03-13
    • 2021-07-19
    • 1970-01-01
    • 2013-07-22
    相关资源
    最近更新 更多