Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Cart' instance needs to have a primary key value before this relationship can be used. #361

Closed
Wladislav opened this issue Aug 8, 2022 · 6 comments · Fixed by #362
Closed
Labels

Comments

@Wladislav
Copy link

Good afternoon.
I just installed the cartridge to see its capabilities and got an error. Please help me solve this.

Django Version: 4.1
Python Version: 3.8.10
Installed Applications:
('mezzanine.boot',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.redirects',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.messages',
'mezzanine.conf',
'mezzanine.core',
'mezzanine.generic',
'mezzanine.pages',
'cartridge.shop',
'mezzanine.blog',
'mezzanine.forms',
'mezzanine.galleries',
'filebrowser_safe',
'grappelli_safe',
'django.contrib.admin',
'django.contrib.staticfiles',
'django_comments')
Installed Middleware:
('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',
'cartridge.shop.middleware.ShopMiddleware',
'mezzanine.core.request.CurrentRequestMiddleware',
'mezzanine.core.middleware.RedirectFallbackMiddleware',
'mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware',
'mezzanine.core.middleware.SitePermissionMiddleware',
'mezzanine.pages.middleware.PageMiddleware')

Template error:
In template /home/vlad/anycoderu/cartridge/cartridge/shop/templates/shop/includes/user_panel.html, error at line 5
'Cart' instance needs to have a primary key value before this relationship can be used.
1 : {% load i18n shop_tags mezzanine_tags %}
2 : {% spaceless %}
3 : <a href="{% url "shop_cart" %}">
4 :
5 : {% blocktrans count request.cart.total_quantity as cart_quantity %} 1 item{% plural %}{{ cart_quantity }} items{% endblocktrans %}
6 : {% trans "in cart" %}:
7 : {{ request.cart.total_price|currency }}

8 : {% if request.cart.total_quantity != 0 %}
9 : <a href="{% url "shop_checkout" %}" class="btn btn-primary">
10 : {% if request.session.order.step %}{% trans "Return to Checkout" %}{% else %}{% trans "Go to Checkout" %}{% endif %}
11 :

12 : {% endif %}
13 : {% if settings.SHOP_USE_WISHLIST %}
14 : <a href="{% url "shop_wishlist" %}" class="btn-wishlist">
15 :

Traceback (most recent call last):
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/core/handlers/base.py", line 220, in _get_response
response = response.render()
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/response.py", line 114, in render
self.content = self.rendered_content
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/response.py", line 92, in rendered_content
return template.render(context, self._request)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/backends/django.py", line 62, in render
return self.template.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 175, in render
return self._render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 167, in _render
return self.nodelist.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 966, in render_annotated
return self.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/loader_tags.py", line 157, in render
return compiled_parent._render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 167, in _render
return self.nodelist.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 966, in render_annotated
return self.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/mezzanine/template/init.py", line 95, in render
args = (self.nodelist.render(context), context, token)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 966, in render_annotated
return self.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/loader_tags.py", line 208, in render
return template.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 177, in render
return self._render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 167, in _render
return self.nodelist.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 966, in render_annotated
return self.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/defaulttags.py", line 322, in render
return nodelist.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 966, in render_annotated
return self.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/loader_tags.py", line 208, in render
return template.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 177, in render
return self._render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 167, in _render
return self.nodelist.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 966, in render_annotated
return self.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/defaulttags.py", line 415, in render
return strip_spaces_between_tags(self.nodelist.render(context).strip())
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 1005, in
return SafeString("".join([node.render_annotated(context) for node in self]))
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 966, in render_annotated
return self.render(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/templatetags/i18n.py", line 160, in render
count = self.counter.resolve(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 715, in resolve
obj = self.var.resolve(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 847, in resolve
value = self._resolve_lookup(context)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/template/base.py", line 914, in _resolve_lookup
current = current()
File "/home/vlad/anycoderu/cartridge/cartridge/shop/models.py", line 643, in total_quantity
return sum(item.quantity for item in self)
File "/home/vlad/anycoderu/cartridge/cartridge/shop/models.py", line 610, in iter
self._cached_items = self.items.all()
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/db/models/fields/related_descriptors.py", line 596, in get
instance_cache[key] = self.related_manager_cls(instance)
File "/home/vlad/anycoderu/anycodenv/lib/python3.8/site-packages/Django-4.1-py3.8.egg/django/db/models/fields/related_descriptors.py", line 634, in init
raise ValueError(

Exception Type: ValueError at /
Exception Value: 'Cart' instance needs to have a primary key value before this relationship can be used.

@damascene
Copy link

I'm getting the exact same issue. on the same line. It worked fine on my local machine but failed on a public server.

@daveroberts0321
Copy link

i am just getting mezzanine spun up prior to installing cartridge for the store portion. Has this been resolved?

@Wladislav
Copy link
Author

image
Nobody helped. I had to fix it myself. Not sure if this is correct, but it works for me.

@chandave
Copy link

@Wladislav: Your fix appears to work. Great work!

However, we might have an issue with cartridge/shop/utils.py:recalculate_cart() in the future. It reference request.cart.pk which might not be defined for all instances. I hope someone with a better understanding of the code would look into the issue and see if you need to wrap it with a check before trying to evaluate it.

@henri-hulski
Copy link
Collaborator

Does this issue only appears on Django 4.1 or also on 4.0?

@github-actions
Copy link
Contributor

🎉 This issue has been resolved in version 1.3.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants