Django SiteProfileNotAvailable error - python

I'm getting the following error from userna: SiteProfileNotAvailable I know what the error means but the path in AUTH_PROFILE_MODULE is correct, so why I'm I getting this error?
Thanks
settings.py
# My apps
INSTALLED_APPS += (
'project.apps.ranger',
)
#userna
ANONYMOUS_USER_ID = -1
AUTH_PROFILE_MODULE = 'project.apps.ranger.Profile'
model:
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from userena.models import UserenaLanguageBaseProfile
class Profile(UserenaLanguageBaseProfile):
"""
Default profile of Ranger
"""
GENDER_CHOICES = (
(1, _('Male')),
(2, _('Female')),
)
EYE_CHOICES = (
(1, _('Blue')),
(2, _('Green')),
(3, _('Brown')),
)
gender = models.PositiveSmallIntegerField(_('gender'),
choices=GENDER_CHOICES,
blank=True,)
user = models.OneToOneField(User,
unique=True,
verbose_name=_('user'),
related_name='user_profile')
Full Error:
SiteProfileNotAvailable at /
No exception supplied
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.5.1
Exception Type: SiteProfileNotAvailable
Exception Location: /Users/user/Documents/workspace/project/django-env/lib/python2.7/site-packages/userena/utils.py in get_profile_model, line 119
Python Executable: /Users/user/Documents/workspace/project/django-env/bin/python
Python Version: 2.7.2
Python Path:
['/Users/user/Documents/workspace/project',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg',
'/Users/user/Documents/workspace/project/django-env/lib/python27.zip',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7/plat-darwin',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7/plat-mac',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/user/Documents/workspace/project/django-env/Extras/lib/python',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7/lib-tk',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7/lib-old',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/user/Documents/workspace/project/django-env/lib/python2.7/site-packages']
File structure is like this:
project
/project
/apps
/settings
/logs
/requirements

I think your problem might be with how you're specifying the AUTH_PROFILE_MODULE setting. Try using ranger.profile instead of project.apps.ranger.profile.
See this blog post.

Strangely, this error often seems to occur for me when I have a circular dependency. For example, when importing models from a django app into a models.py file which is itself imported into the first one. If you're running into this error, check that you are not doing a circular import.

Related

Python/Django 'OperationalError' in admin Django Rest Framework (DRF)

I keep getting the Operational error below in my Django admin when I try to update a models table.
I'm trying to add a new field to the polls table.
What could be the problem?
Thanks in advance!
OperationalError at /admin/vote/poll/add/
no such table: main.auth_user__old
Request Method: POST
Request URL: http://localhost:8000/admin/vote/poll/add/
Django Version: 2.0.3
Exception Type: OperationalError
Exception Value: no such table: main.auth_user__old
Exception Location: D:\Studio\Python\REST\elections\env\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 303
Python Executable: D:\Studio\Python\REST\elections\env\Scripts\python.exe
Python Version: 3.9.0
Python Path:
The polls model: models.py
class Poll(models.Model):
question = models.CharField(max_length=100)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
pub_date = models.DateTimeField(auto_now=True)
The urls.py code:
urlpatterns = [
path("polls/", PollList.as_view(), name="polls_list"),
path("polls/<int:pk>/", PollDetail.as_view(), name="polls_detail"),
]
Is there any specific reason you are using Django v2 instead of v3.
You could do a couple of things to resolve this:
Upgrade Django version to 2.2 or above.
Downgrade SQLite version if you are using one.
After that you can delete the old database, and run you migrations again:
python manage.py makemigrations
python manage.py migrate

Django on Pycharm community edition seems to show false-positive error message

I'm new to Django and I'm using Pycharm Community 2018.1 to do my project.
While I was doing the first tutorial where you set up a model and corresponding admin page, Pycharm keeps showing this reference error which is perfectly fine when you look at the directory and the structure.
Is this what I get for using free Pycharm instead of paid Professional edition?
However, I know I'm a newb so I might be wrong here. Is it normal to get this error message in this situation?
I took a screenshot of error message(the left screen) and working Admin page(right screen) and here are my codes for models.py and admin.py
models.py (there is no problem with it)
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
# Create your models here.
#python_2_unicode_compatible
class Bookmark(models.Model):
title = models.CharField(max_length = 100, blank = True, null = True)
url = models.URLField('url', unique = True)
def __str__(self):
return self.title
admin.py (you can see unresolved reference error)
from django.contrib import admin
from bookmark.models import Bookmark
# Register your models here.
class BookmarkAdmin(admin.ModelAdmin):
list_display = ('title', 'url')
admin.site.register(Bookmark, BookmarkAdmin)
This warning can be solved by opening your actual project directory
Go to File->Open. Then choose your project directory, that is mysite and you are opened pyneer_django directory
It's nothing with the community edition

Removing django default permissions from auth models

guys im trying to remove default django permissions i'll never use in my project but with no success. When I make migration it states that migration is made with success but there's no effect, like it skipped the function. I'm pretty sure code is ok because i tested it in shell. Any ideas?
Here's code for migration:
from django.db import migrations
def remove_redundant_permissions(apps, schema_editor):
Permission = apps.get_model('auth.Permission')
app_labels = ['admin', 'reversion', 'contenttypes', 'sessions', 'sites']
Permission.objects.filter(content_type__app_label__in=app_labels).delete()
class Migration(migrations.Migration):
dependencies = [
('users', '0014_auto_20160808_0738'),
]
operations = [
migrations.RunPython(remove_redundant_permissions),
]
Django's permessions are generated with post_migrate signal. This means even if you delete them in migration they will be regenerated after migration completes.
Here is code from django
# django/contrib/auth/apps.py
from django.apps import AppConfig
from django.contrib.auth.checks import check_user_model
from django.core import checks
from django.db.models.signals import post_migrate
from django.utils.translation import ugettext_lazy as _
from .management import create_permissions
class AuthConfig(AppConfig):
name = 'django.contrib.auth'
verbose_name = _("Authentication and Authorization")
def ready(self):
post_migrate.connect(create_permissions,
dispatch_uid="django.contrib.auth.management.create_permissions")
checks.register(check_user_model, checks.Tags.models)
As you see auth app has this AppConfig which regenerates permissions with create_permissions function.
Why do you want to delete default django permissions? Do they block you from doing something?

Import model in Django's settings.py

I'm trying to define a constant with the value of model's class in the settings.py to provide a dynamically-defined FK for one of my models:
from catalog.models import Product
PRODUCT_MODEL = Product
No surprise it's leading to an AppRegistryNotReady exception:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
since there was no call to django.setup() method.
If I add
import django
django.setup()
in front of the model's import, instead of AppRegistryNotReady exception, I'm getting
AttributeError: 'Settings' object has no attribute 'PRODUCT_MODEL'
when I'm using
from django.conf import settings
...
product = models.ForeignKey(
settings.PRODUCT_MODEL,
related_name='order_items')
Is there any way to implement this without errors?
I'm using Python 3.5 and Django 1.9.5.
You shouldn't import models or call django.setup() in your settings file.
Instead, use a string to refer to your model.
PRODUCT_MODEL = 'catalog.Product'
Then, in your models, you can use the setting.
product = models.ForeignKey(
settings.PRODUCT_MODEL,
related_name='order_items',
)
Note that the AUTH_USER_MODEL setting uses a string like this, you do not import the user model in the settings file.
Store the model name as a string.
In settings.py:
PRODUCT_MODEL = 'YourModel'
In your models.py:
from django.apps import AppConfig
....
field = models.ForeignKey(AppConfig.get_model(PRODUCT_MODEL))
(This works from Django 1.9.)
Use catalog.Product in settings and lazy import

Django complains that django.contrib.gis.measure.Distance is not a valid model

Environment details:
Ubuntu GNOME 14.04
Python 2.7.6 (using a virtualenv)
Django 1.7 (inside the virtualenv)
Eclipse Kepler SR2 (build id 20140224-0627)
PyDev 3.8.0.201409251235
I have the following in a model:
from django.db import models
from django.contrib.gis.measure import Distance
...
class MyModel(models.Model):
...
distance = models.ForeignKey(Distance)
...
The model is located in /home/user/workspace/DjangoProject/MyApp/models/mymodel.py. In the models folder, I have an __init__.py which imports MyModel:
from MyApp.models.mymodel import MyModel
When I try to run the server (python manage.py runserver) or sync the models to the database (python manage.py syncdb), I get the following error:
AssertionError: ForeignKey(<class 'django.contrib.gis.measure.Distance'>) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string u'self'
I checked django/contrib/gis/measure.py and the Distance model inside appears to be a valid model. Why does Django think that Distance is not a valid model?
django.contrib.gis.measure import Distance is not a Model, it is just a class that extends MeasureBase that extends object.
class MeasureBase(object):¬
STANDARD_UNIT = None¬
ALIAS = {}¬
UNITS = {}¬
LALIAS = {}¬
So, "Why does Django think that Distance is not a valid model?" - because it isn't a model. Models in Django are created by extending models.Model.

Categories

Resources