AttributeError:'...'对象没有属性'*** _ set'

时间:2017-11-03 13:31:36

标签: python django

我正在尝试显示分配给“个人资料”的“统计信息”列表,这些信息扩展了用户模型。我有一个ForeignKey将Stat与简档相关联。但是,我需要它才能工作,因此用户无需登录,因此他们可以与不是该站点成员的用户共享进度。

我正在尝试将_set用于ForeignKey上的向后关系。我认为它的绊倒是因为'Stat'模型和'Profile'模型在不同的应用程序中。这会导致这个问题吗?代码如下:

class Stat(models.Model):
    user = models.ForeignKey(User, default=False)
    image = CloudinaryField('image', default="thumbnail_mqe6ne", blank=True)


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

//个人资料视图

def profile_item(request, id):
    p = Profile.objects.get(id=7)
    user_stats = p.stat_set.all()

    context = {
        "p": p,
        "user_stats": user_stats,

    }
    return render(request,"profile/profile_share.html", context)

我收到的观点:

'Profile' object has no attribute 'stat_set'

据我所知,使用向后引用时,您需要以小写形式指定要定位的模型。下面的完整堆栈跟踪:

环境:

请求方法:GET 请求网址:http://localhost:8000/profile/7/

Django Version: 1.10
Python Version: 2.7.14
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.sites',
 'stats',
 'home',
 'blog',
 'challenge',
 'haystack',
 'ckeditor_uploader',
 'ckeditor',
 'django_cron',
 'anymail',
 'templated_email',
 'django_social_share',
 'sorl.thumbnail',
 'storages',
 'django.contrib.staticfiles',
 'cloudinary_storage',
 'cloudinary',
 'debug_toolbar',
 'django_cleanup',
 'django_instagram',
 'embed_video',
 'easy_thumbnails',
 'filer',
 'reversion',
 'bootstrap3',
 'rest_framework',
 'meta',
 'import_export',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.facebook']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'solid_i18n.middleware.SolidLocaleMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.middleware.locale.LocaleMiddleware']



Traceback:

File "/vagrant/bodymakeover/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/vagrant/bodymakeover/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/vagrant/bodymakeover/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/vagrant/bodymakeover/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/vagrant/bodymakeover/home/views.py" in profile_item
  117.     user_stats = p.stat_set.all()

Exception Type: AttributeError at /profile/7/
Exception Value: 'Profile' object has no attribute 'stat_set'

非常感谢。

2 个答案:

答案 0 :(得分:3)

ProfileStat彼此无关 - 它们都与User相关。您应该从User模型执行此反向查找,或者创建从StatProfile的ForeignKey关系。

答案 1 :(得分:2)

@souldeux有权利。如果你想要Profile的搜索统计数据,你应该建立像

这样的Queryset
p = Profile.objects.get(id=7)
user_stats = p.user.stat_set.all()