Reputation: 2541
I'm learning django from thedjangobook and there is an example at class based views that it's in the django documentations aswell, here, my problem is that i'm getting an error when i'm trying to run this.
It is supposed to keep track of the last time anybody looked at an author:
models.py
class Author(models.Model):
salutation = models.CharField(max_length=10, null=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField(blank=True, verbose_name='e-mail')
headshot = models.ImageField(upload_to='author_headshots', null=True, blank=True)
last_accessed = models.DateTimeField(null=True)
urls.py
urlpatterns = [
url(r'^authors/(?P<pk>[0-9]+)/$', views.AuthorDetailView.as_view(), name='AuthorDetailView'),
]
views.py
class AuthorDetailView(DetailView):
def get_object(self, queryset=Author.objects.all()):
# Call the superclass
object_1 = super(Author, self).get_object()
# Record the last accesed date
object_1.last_accessed = timezone.now()
object_1.save()
# Return the object
return object_1
Error:
Traceback (most recent call last):
File "/home/alex/Documents/Proiecte/Django/Django_tutorial/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/home/alex/Documents/Proiecte/Django/Django_tutorial/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/alex/Documents/Proiecte/Django/Django_tutorial/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/alex/Documents/Proiecte/Django/Django_tutorial/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/alex/Documents/Proiecte/Django/Django_tutorial/venv/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/alex/Documents/Proiecte/Django/Django_tutorial/venv/lib/python3.5/site-packages/django/views/generic/detail.py", line 115, in get
self.object = self.get_object()
File "/home/alex/Documents/Proiecte/Django/Django_tutorial/mysite/books/views.py", line 179, in get_object
object_1 = super(Author, self).get_object()
TypeError: super(type, obj): obj must be an instance or subtype of type
Upvotes: 0
Views: 2770
Reputation: 43300
Your get_object
method is trying to call super, but you need to pass in the current class name rather than the model name
object_1 = super(Author, self).get_object()
should be either
object_1 = super(AuthorDetailView, self).get_object()
object_1 = super().get_object() # The args can be omitted for Python 3
Upvotes: 4