2017-11-03 4 views
0

사용자 모델을 확장하는 '프로필'에 할당 된 '통계'목록을 표시하려고합니다. 저는 Stat와 프로파일을 연관 짓는 ForeignKey를 가지고 있습니다. 그러나 사용자가 사이트의 구성원이 아닌 사용자에게 진행 상황을 공유 할 수 있도록 로그인 할 필요가 없도록 작동해야합니다.AttributeError : '...'객체에 '*** _ set'속성이 없습니다.

ForeignKey에서 뒤로 설정을 위해 _set을 사용하려고합니다. '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 요청 URL : 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' 

많은 감사합니다.

답변

3

ProfileStat은 서로 관련이 없습니다. 둘 다 User과 관련이 있습니다. User 모델에서이 역방향 조회를 수행하거나 Stat에서 Profile까지 ForeignKey 릴레이션을 작성해야합니다.

+0

감사합니다. 완벽하게 이해합니다. 나는이 문제를 간과했다. – JDavies

2

@souldeux가 있습니다. 프로필에 대한 검색 통계를 원할 경우

과 같은
p = Profile.objects.get(id=7) 
user_stats = p.user.stat_set.all() 
+0

완벽하게 작동합니다. 고맙습니다! – JDavies

관련 문제