Django 1.9 and django.contrib.auth - django

I don't have django.contrib.auth in INSTALLED_APPS, and I don't want to do it because I'm using a custom user model and I don't need none of the builtin User, Group and Permission models.
I was hoping this was possible in Django 1.9, as the docs state in https://docs.djangoproject.com/en/1.9/releases/1.9/ under the django.contrib.auth section that:
AbstractBaseUser and BaseUserManager were moved to a new django.contrib.auth.base_user module so that they can be imported without including django.contrib.auth in INSTALLED_APPS (doing so raised a deprecation warning in older versions and is no longer supported in Django 1.9).
However, as of now I can't import anything from django.contrib.auth without raising errors. No middleware or form can be imported. I have to either add django.contrib.auth to INSTALLED_APPS or don't use anything from that module.
Whenever a model in django.contrib.auth is indirectly imported I get the classic error that: whatever_model_was_indirectly_imported doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
EDIT: Including last part of the traceback:
...
File "/usr/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 3, in <module>
from django.contrib.auth.backends import RemoteUserBackend
File "/usr/lib/python2.7/site-packages/django/contrib/auth/backends.py", line 4, in <module>
from django.contrib.auth.models import Permission
File "/usr/lib/python2.7/site-packages/django/contrib/auth/models.py", line 38, in <module>
class Permission(models.Model):
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 102, in __new__
"INSTALLED_APPS." % (module, name)
RuntimeError: Model class django.contrib.auth.models.Permission doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
Any workarounds?

It looks like the only workaround as of now is including django.contrib.auth in INSTALLED_APPS and ignoring the three tables in database.

Related

ModuleNotFoundError for model class in django

I ran into the following ModuleNotFoundError error.
File "C:\django-project\CodingWithMitchBlog\demoproject\Blog\api\urls.py", line 3, in <module>
from demoproject.Blog.api.views import api_detail_BlogPost_view
ModuleNotFoundError: No module named 'demoproject.Blog'
'Blog' is my appname.
This is my project structure.
This might be due to incorrect import statements used in the files. If the import statements has the projectname prefixed try removing it and run again.
For example change:
from demoproject.Blog.models import ModelClass
to
from Blog.models import ModelClass
in the urls.py file

Django-inline-media get_model error

I am using Django 1.9 and for my project I need django-inline-media. I have done all stuff properly as per documentation. But I get this error.
File "/home/sifat/PycharmProjects/fear/profiles/models.py", line 3, in <module>
from inline_media.fields import TextFieldWithInlines
File "/home/sifat/PycharmProjects/fear/env/local/lib/python2.7/site- packages/inline_media/fields.py", line 4, in <module>
from django.db.models import fields, get_model
ImportError: cannot import name get_model
Now what to do? As it is error from a library
django.db.models.get_model has been deprecated in Django 1.7 and was removed in 1.9.
In Django 1.9, you can replace it with
from django.apps import apps
model = apps.get_model('app_name', 'model_name')

Django on Pycharm: ImproperlyConfigured with DJANGO_SETTINGS_MODULE

I am trying to use Pycharm Community Edition to improve on my code in my Django application, but I cannot run all of my Django code that I'd like. I keep getting this traceback...
Traceback (most recent call last):
File "C:/Users/Jaysp_000/firstSite/PROJECTone/blog_static/views.py", line 1, in <module>
from django.views.decorators.csrf import csrf_exempt
File "C:\Python34\lib\site-packages\django\views\decorators\csrf.py", line 3, in <module>
from django.middleware.csrf import CsrfViewMiddleware, get_token
File "C:\Python34\lib\site-packages\django\middleware\csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
File "C:\Python34\lib\site-packages\django\utils\cache.py", line 26, in <module>
from django.core.cache import caches
File "C:\Python34\lib\site-packages\django\core\cache\__init__.py", line 34, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
This error seems to involve the django.views.decortors.csrf.csrf_exempt that I imported to my views.py file. I've tried other files, and they have given me no issues. There is something in particular about this import, but I don't know what.
from django.views.decortors.csrf import csrf_exempt
#csrf_exempt
def handle_hook(request):
from django.http import HttpResponse
from django.core.management import call_command
result = call_command('update_blog', verbosity = 0)
return HttpResponse(result)
The same kind of issue shows up when I am trying to run the code on the python shell (I use 3.4) and when I import django.http.request as request. I type in handle_hook(request), and I get the same kind of error.
Im being told that I must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings, but I haven't a clue on how to do that. I've looked around and I am not certain if those methods specifically speak to my issue. Any clues?
Go to the Run Menu, select Edit Configurations..., then select the run configuration for you tests.
Select the environment variables button. You'll see one existing variable, which is PYTHONUNBUFFERED
Under that add (for example) DJANGO_SETTINGS_MODULE=mysitename.settings

manage.py shell and relative imports

I'm trying to load the following Python file with "manage.py shell". The manage.py script is in a parent directory and I'm using execfile('forms.py') from the directory containing the script.
from django.forms import ModelForm
from .models import Profile
class ProfileSearchForm(ModelForm):
class Meta:
model = Profile
fields = ['name']
This fails because of the explicit relative import:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "forms.py", line 2, in <module>
from .models import Profile
ImportError: No module named models
but if I switch to implicit relative import, it works:
from django.forms import ModelForm
from apps.myprofile.models import Profile
class ProfileSearchForm(ModelForm):
class Meta:
model = Profile
fields = ['name']
My problem is that I thought that explicit relative imports are a good thing (see Two Scoops), and I still think they are.
But now I need a workaround with manage.py.
you need to change the current directory before the relative import in python shell. So this may works:
import os
os.chdir('apps/')
from django.forms import ModelForm
from .models import Profile

ImportError: No module named models

I am going a tad crazy here. I keep getting this error: ImportError: No module named models and I am not sure why. Here is what I have found so far...
>>> from django.shortcuts import get_object_or_404, redirect
>>> from mystore.cart import cart
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/Jeff/django/mystore/cart/cart.py", line 3, in <module>
from mystore.cart.models import CartItem
ImportError: No module named models
>>>
I am not sure what's going on with this... line 3 in cart.py is this:
from mystore.cart.models import CartItem
If I try to do: from mystore.cart.models import CartItem it works fine...
Any suggestions?
Almost certainly, you have a circular dependency: mystore.cart.cart is importing mystore.cart.models, which in turn is trying to import mystore.cart.cart.
You should determine if both of those imports are necessary, and if either of them could be moved out of the global scope into a function or method.
Why are you doing from mystore.cart import cart? That should be just from mystore import cart.
Very early in the mystore.cart.models an error is occurring that's why nothing in models.py can be imported. The error can be a circular import, a conditional statement that's triggered during runtime but not at the command prompt or is happening inside something else your are importing at the beginning of models.py
You have to put a point before.
bad
from models import *
good
from .models import *
that means that is at the same level.