I am building this rest api which gets data from database and shows all the fields regarding the books in database.
models.py
code:
class Books(models.Model):
Name = models.CharField(max_length=250)
Author = models.ForeignKey('Authors', on_delete=models.CASCADE, default=None)
Published_Date = models.DateField(blank=False)
Pages = models.IntegerField()
critics = models.IntegerField(default=0)
def __str__(self) -> str:
return self.Name
serializer.py
code:
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Books
fields = ['Name', 'Author', 'Published_date', 'Pages', 'critics']
urls.py
code:
urlpatterns = [
path('books/', views.BookList.as_view()),
path('Bdetails/<str:pk>', views.BookDetail.as_view()),
]
views.py
code:
class BookList(generics.ListCreateAPIView):
queryset = Books.objects.all()
serializer_class = BookSerializer
class BookDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Books.objects.all()
serializer_class = BookSerializer
traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/books/
Django Version: 3.2.9
Python Version: 3.9.6
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'genebox',
'rest_framework']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'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']
Traceback (most recent call last):
File "C:\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in
_get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python39\lib\site-packages\django\views\decorators\csrf.py", line 54, in
wrapped_view return view_func(*args, **kwargs)
File "C:\Python39\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Python39\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Python39\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Python39\lib\site-packages\rest_framework\views.py", line 480, in
raise_uncaught_exception
raise exc
File "C:\Python39\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Python39\lib\site-packages\rest_framework\generics.py", line 239, in get
return self.list(request, *args, **kwargs)
File "C:\Python39\lib\site-packages\rest_framework\mixins.py", line 43, in list
return self.get_paginated_response(serializer.data)
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 745, in data
ret = super().data
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 246, in data
self._data = self.to_representation(self.instance)
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 663, in
to_representation
return [
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 664, in <listcomp>
self.child.to_representation(item) for item in iterable
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 500, in
to_representation
for field in fields:
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 361, in
_readable_fields
for field in self.fields.values():
File "C:\Python39\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 349, in fields
for key, value in self.get_fields().items():
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 1053, in get_fields
field_class, field_kwargs = self.build_field(
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 1199, in
build_field
return self.build_unknown_field(field_name, model_class)
File "C:\Python39\lib\site-packages\rest_framework\serializers.py", line 1317, in
build_unknown_field
raise ImproperlyConfigured(
Exception Type: ImproperlyConfigured at /books/
Exception Value: Field name `Published_date` is not valid for model `Books`.
Your Help will be appreciated
CodePudding user response:
Your field is named Published_Date
with an uppercase D
, not . But normally fields are not written with uppercases, but in Published_date
snake_case
, and model names are singular, so Book
, not , and Books
Author
, not :Authors
class Book(models.Model):
name = models.CharField(max_length=250)
author = models.ForeignKey('Author', on_delete=models.CASCADE, default=None)
published_date = models.DateField()
pages = models.IntegerField()
critics = models.IntegerField(default=0)
def __str__(self) -> str:
return self.name