【问题标题】:AttributeError: '…' object has no attribute '***_set'AttributeError: '...' 对象没有属性 '***_set'
【发布时间】:2017-11-03 13:31:36
【问题描述】:

我正在尝试显示分配给扩展用户模型的“配置文件”的“统计信息”列表。我有一个将 Stat 与配置文件相关联的 ForeignKey。但是,我需要它来工作,这样用户就不必登录,这样他们就可以与非网站成员的用户分享进度。

我正在尝试将 _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'

非常感谢。

【问题讨论】:

    标签: python django


    【解决方案1】:

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

    【讨论】:

    • 谢谢!!完全有道理。我忽略了这个问题。
    【解决方案2】:

    @souldeux 是对的。如果你想要 Profile 的搜索统计信息,你应该构建 Queryset 像

    p = Profile.objects.get(id=7)
    user_stats = p.user.stat_set.all()
    

    【讨论】:

    • 完美运行。谢谢!
    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    相关资源
    最近更新 更多