N
N
No_name4512021-03-12 15:08:11
Django
No_name451, 2021-03-12 15:08:11

How to fix update/delete data error via generics (django)?

It is not possible to delete or update data through the default junga generics. Please give me a specific answer on how to solve this. Thank you in advance.
Files:

models.py

from django.db import models as md


class City(md.Model):
    name = md.CharField(max_length=150, unique=True, verbose_name='Город')

    def __str__(self):
        return f'{self.name}'

    class Meta:
        verbose_name = 'Город'
        verbose_name_plural = 'Города'
        ordering = ['name']

views.py
from cities.models import City
from cities.forms import NewCity
from django.views.generic import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls.base import reverse_lazy


class IndexView(ListView):
    model = City
    context_object_name = 'cities'
    template_name = 'cities/index.html'


class City(DetailView):
    queryset = City.objects.all()
    context_object_name = 'city'
    template_name = 'cities/city.html'


class CreateCity(CreateView):
    model = City
    form_class = NewCity
    template_name = 'cities/create.html'
    success_url = reverse_lazy('cities:index')


class UpdateCity(UpdateView):
    model = City
    form_class = NewCity
    template_name = 'cities/update.html'
    success_url = reverse_lazy('cities:index')


class DeleteCity(DeleteView):    
    model = City
    template_name = 'cities/delete.html'
    success_url = reverse_lazy('cities:index')

urls.py
from django.urls import path
from cities import views


urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('city/<int:pk>', views.City.as_view(), name='city'),
    path('create/', views.CreateCity.as_view(), name='create'),
    path('update/<int:pk>', views.UpdateCity.as_view(), name='update'),
    path('delete/<int:pk>', views.DeleteCity.as_view(), name='delete'),
]

There are no problems when working with IndexView, City, CreateCity (IndexView displays a specific list; City - the required item from the list, CreateCity - creates a new entry, after which it redirects to the main page). When trying to edit or delete an entry, the same error appears.
  • Removal:

AttributeError at /cities/delete/22
type object 'City' has no attribute '_default_manager'

  • Update:

AttributeError at /cities/update/22
type object 'City' has no attribute '_default_manager'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mikeyuri, 2021-03-12
@No_name451

Name conflict

from cities.models import City

classCity(DetailView):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question