Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

type object 'PizzaMenu' has no attribute '_default_manager'

I have following error while i try to reach PizzaDetailView on template: AttributeError at /pizza/6/
type object ‘PizzaMenu’ has no attribute ‘_default_manager’. Where is the problem?

models.py

class PizzaMenu(models.Model):
    name = models.CharField(max_length=30)
    description = models.TextField()
    ingredients = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=4, decimal_places=2)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ["price"]

views.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from django.views.generic import TemplateView, ListView, DetailView
from .models import PizzaMenu


class IndexView(TemplateView):
    template_name = "index.html"


class PizzaMenu(ListView):
    model = PizzaMenu
    template_name = "menu.html"
    context_object_name = "pizza_menu"

    # ordering = ["price"]


class PizzaDetailView(DetailView):
    model = PizzaMenu
    template_name = "pizza_detail.html"
    context_object_name = "pizza_detail"


class About(TemplateView):
    template_name = "about.html"

urls.py

from pizza.views import IndexView, PizzaMenu, About, PizzaDetailView

urlpatterns = [
    path("", IndexView.as_view(), name="home"),
    path("menu/", PizzaMenu.as_view(), name="menu"),
    path("about/", About.as_view(), name="about"),
    path("pizza/<int:pk>/", PizzaDetailView.as_view(), name="pizza_detail"),
    path("admin/", admin.site.urls),
]

>Solution :

Don’t name your view PizzaMenu, it will override the reference to the PizzaMenu for the other views. Usually class-based views have a …View suffix, so:

class IndexView(TemplateView):
    template_name = 'index.html'


# add a View suffix to prevent collissions with the PizzaMenu model class
class PizzaMenuView(ListView):
    model = PizzaMenu
    template_name = 'menu.html'
    context_object_name = 'pizza_menu'


class PizzaDetailView(DetailView):
    model = PizzaMenu
    template_name = 'pizza_detail.html'
    context_object_name = 'pizza_detail'


class AboutView(TemplateView):
    template_name = 'about.html'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading