I'm trying to display a list of 'stats' assigned to a 'Profile' which extenders the User model. I have a ForeignKey relating the Stat to the Profile. However, i need it to work so the user doesn't have to be logged in so they share progress to users who are not a member of the site.
I'm trying to use _set for a backwards relationship on the ForeignKey. I think where it's tripping up is because the 'Stat' model and the 'Profile' models are inside different apps. Would this be causing the issue? Code below:
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)
// Profile view
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)
The view i'm receiving:
'Profile' object has no attribute 'stat_set'
As far as i'm aware using the backwards reference you need to specify the model you are targeting in lowercase. Full stack trace below:
Environment:
Request Method: GET Request 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'
Many thanks.
Profile
and Stat
aren't related to one another - they're both related to User
. You should perform this reverse lookup from the User
model, or create a ForeignKey relation from Stat
to Profile
.
@souldeux has right. And if you want search stats for Profile you should build Queryset like
p = Profile.objects.get(id=7)
user_stats = p.user.stat_set.all()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.