I am using python 3.4, Django 1.7.1 (the version considered in the book), Postgres 9.3 and my IDE is Eclipse.
I have been studying the book "Lightweight Django - Elman and Lavin" and I have been stuck for days in the chapters 4 and 5, where we are supposed to use the rest framework and backbone.js. See for instance,
Lightweight Django - Chapters 4 and 5
Some days ago, I tried to code by myseld as presented in the book and also checking with the examples presented in the link above. However, since I was not going ahead, I decided to copy the code presented in the link above and tried to run. The same error has arisen:
AssertionError at / Relational field must provide a `queryset` argument, or set read_only=`True`. Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 1.7.1 Exception Type: AssertionError Exception Value:
Relational field must provide a queryset
argument, or set read_only=True
.
Exception Location: /usr/local/lib/python3.4/dist-packages/rest_framework/relations.py in __init__, line 35 Python Executable: /usr/bin/python3 Python Version: 3.4.0 Python Path: ['/home/daniel/workspace/Scrum', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-i386-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
This error arises inside "relations.py, which belongs to the django-rest-framework. Since I am using the exact code presented in the link above, it is supposed to not have errors. Actually, the only piece of code that I changed was in the settings.py (after the error repeatedly happened):
Before:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'scrum', } }
Now:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'scrum', 'USER': 'daniel', 'PASSWORD': '12345', 'HOST': '127.0.0.1', 'PORT': '5432', }
As you can see below, my user "daniel" has the following attributes:
Role name | Attributes | Member of | Description -----------+------------------------------------------------+-----------+------------- daniel | Superuser, Create DB | {} | postgres | Superuser, Create role, Create DB, Replication | {} |
Finally, it seems that I do not have any problem with the installation of psycopg2, since I was able to create "scrum" as presented below. In fact, the list of databases of my system is
List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | scrum | daniel | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/daniel + | | | | | daniel=CTc/daniel template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres
Can someone help me to discover the problem?
The function must take a pair of arguments, the first is the exception to be handled, and the second is a dictionary containing any extra context such as the view currently being handled. The exception handler function should either return a Response object, or return None if the exception cannot be handled.
The easiest way to change the error style through all the view in your application is to always use serializer. is_valid(raise_exception=True) , and then implement a custom exception handler that defines how the error response is created.
I want to return a HTTP 400 response from my django view function if the request GET data is invalid and cannot be parsed. Return a HttpResponseBadRequest : docs.djangoproject.com/en/dev/ref/request-response/… You can create an Exception subclass like Http404 to have your own Http400 exception.
Read the DRF docs here.
In version 2.x a serializer class could sometimes automatically determine the queryset argument if a ModelSerializer class was being used.
This behavior is now replaced with always using an explicit queryset argument for writable relational fields.
You are just using a newer version of DRF than the authors of the code used, so you'll need to either use a lower version or fix the code.
In serializers.py there's this line:
assigned = serializers.SlugRelatedField(slug_field=User.USERNAME_FIELD, required=False)
You need to either add read_only=True
or queryset=User.objects.all()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With