Cannot import serializers.ListSerializer - django

When trying to use ListSerializer, according to this doc, I get an error:
Cannot find reference 'ListSerializer' in 'serializers.py'
Here is my import statement:
from rest_framework import serializers
What am I doing wrong?

Make sure you are using Django REST Framework 3. The ListSerializer was added in DRF 3 and did not exist in earlier version.
You can check this using pip freeze, and you should see a line similar to...
djangorestframework==3.0
...in the the output. If you are using an earlier version, you can upgrade to get the new ListSerializer, as well as many other features that were recently introduced.

Related

Django: single related object descriptor is deprecated, how can i replace it?

I was following this guide, when I arrived to this line:
from django.db.models.fields.related import SingleRelatedObjectDescriptor
I realized that SingleRelatedObjectDescriptor Class has been removed (deprecated) from the module,
I searched about its deprecation in the documentation, I couldn't find anything helpful, how i can achieve its behaviour now? with what i can replace it?
It looks like it is mentioned in the django 1.9 release notes
The related model object descriptor classes in
django.db.models.fields.related (private API) are moved from the
related module to related_descriptors and renamed as follows:
ReverseSingleRelatedObjectDescriptor is ForwardManyToOneDescriptor
SingleRelatedObjectDescriptor is ReverseOneToOneDescriptor
ForeignRelatedObjectsDescriptor is ReverseManyToOneDescriptor
ManyRelatedObjectsDescriptor is ManyToManyDescriptor
So I would assume that you need to do:
from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor

Moving from Django 1.6.x to 1.9.x import model errors

I have a few defined apps in my Django project, each with their own sub-directory (created with startapp)
In the views.py of app1 I have an import to a model from app2
from app2.models import MyModel
This worked in Django 1.6.x. In version 1.9 I get:
Could not resolve variable
sometimes on MyModel, sometimes on the filter(..) method, or on both.
If I change the import to
from app2.models import * ##UnusedWildImport
then everything works just fine.
Has anything changed in 1.9.x (or before) that requires a different mode of importing models external to the app?
I think I can rule our circular import problems as this would have failed in 1.6...
Edit: Based on the comments I started wondering whether this might be a PyDev problem.
I tried:
Removing and re-adding Python to PyDev - it did not help
This https://stackoverflow.com/a/8534599/5958359 - removing the myproject/src folder from PYTHONPATH worked ... with a caveat.
The error did not appear when I completely removed the import statement, so this is not a good solution
This is a PyDev error.
Searches haven't yielded an adequate solution - most simply explain how to disable the error - so I will not link to any solution here.
My workaround, as much as I don't like from xxx import * seems like the best temporary solution.

◈ LoginRequired for the view 💈 Django 1.8

I have been using
the braces LoginRequired Mixin in the past, Now it does not seem to work. No errors just you can see the view without Logging in.
Is this the same library?
It seems that a lot of things have changed in django since the last time I have used it. thanks. 
You can use official django login_required method.
from django.contrib.auth.decorators import login_required
class SomeClassView(View):
#classmethod
def as_view(cls, **initkwargs):
view = super(SomeClassView, cls).as_view(**initkwargs)
return login_required(view)
Documentation: https://docs.djangoproject.com/en/1.8/topics/auth/default/#the-login-required-decorator
Yes, this is the same library. The ReadTheDocs link that you posted links directly to the brack3t/django-braces page linked in your question.
Regarding versioning, here is the statement from the Readme.md file:
Our policy going forward is that django-braces officially supports the current version of Django and one version each direction (e.g. 1.6.x is current, so 1.5.x, 1.6.x, and 1.7.x are all supported). There won't be any restraints on using other versions of Django, though, but it will be a "buyer beware" situation.
According to the repo's releases page, v1.8 is current, so it should support Django 1.7.x, 1.8.x, and 1.9.x under the current release.

Django 1.7 tutorial: Use generic views

I need to use Jython instead of Python, I found that jython2.7b2 works with DJango 1.7. So, I am stuck using the beta version. I am trying to follow the current Django tutorial and I have ran into a problem. I am not sure if I am using generic views properly. When I try to change the urls.py (polls) file. I see that pydev complains that views.IndexView, views.DetailView, and ResultsView don't exist. Am I doing something wrong? Or did they change the way generics work in version 1.7?
My System:
Windows 7
jython2.7b2
Django-1.7c3
postgresql-9.3.5-1-windows-x64
postgresql-9.3-1102.jdbc41.jar
Here is the url for the tutorial, go to the section "Use generic views: Less code is better"
Django tutorial part 04
Once you have changed your urls.py, you need to define the new views.
This is covered in the next section of the tutorials, Amend views.

What is this code snippet doing? (from Google App Engine Django example)

I was wondering what this snippet exactly does?
(found here: http://code.google.com/p/google-app-engine-samples/source/browse/trunk/django_example/django_bootstrap.py)
# Make sure we can import Django. We may end up needing to do this
# little dance, courtesy of Google third-party versioning hacks. Note
# that this patches up sys.modules, so all other code can just use
# "from django import forms" etc.
try:
from django import v0_96 as django
except ImportError:
pass
As I stated in another question before, you can use different django versions in app engine (starting from 0.96 up to 1.2 currently). By default it is still using 0.96 as django (and this is what this code snippet does). Though you can change this by adding something like the following to your main.py:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')