I have Anaconda installed in my system but clearly I have set up a new and clean environment for a django API project. I also have all the necessary requirements installed for the API. But when I try to import rest_framework it gives me an error saying "Unable to import 'rest_framework' " even when I have done the following:
#settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'api',
]
#serializers.py
from rest_framework import serializers #<---- this line is giving me error
from .models import Plans
class PlansSerializer(serializers.ModelSerializer):
"""Serializer to map the Model instance into JSON format."""
class Meta:
"""Meta class to map serializer's fields with the model fields."""
model = Plans
fields = ('id', 'name', 'date_created', 'date_modified')
read_only_fields = ('date_created', 'date_modified')
requirements.txt
certifi==2018.11.29
Django==2.1.7
django-crispy-forms==1.7.2
django-filter==2.1.0
django-markdown==0.8.4
django-markdown2==0.3.1
django-pagedown==1.0.6
djangorestframework==3.9.2
djangorestframework-simplejwt==4.1.0
Markdown==3.0.1
markdown2==2.3.7
olefile==0.46
Pillow==5.4.1
Pygments==2.3.1
PyJWT==1.7.1
pytz==2018.9
I also always deactivate the conda's base environment and only activate the required environment.
Related
I have a installed an app called 'Login', who have a folder 'models' inside, with a custom_user model. The problem occurs when I tried to configure settings.py, specially auth_user_model.
in installed apps I have the following:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'project_app.login'
]
and below
AUTH_USER_MODEL = 'login.models.CustomUser'
But I have the following error: "Invalid model reference. String model references must be of the form 'app_label.ModelName'." I put the .models in AUTH_USER_MODEL, because I want to reference the app that the CustomUser is inside the folder "models" in Login.
Also, I tried with declare like this:
AUTH_USER_MODEL = 'login.CustomUser'
but the error is this: 'AUTH_USER_MODEL refers to model 'login.CustomUser' that has not been installed'
The issue is with the way that your app is installed - based on your AUTH_USER_MODEL, login should be the name of the app. It is acceptable to contain Django apps within folders for organisation purposes - however, your parent folder is project_app which from the name also appears to be an app. It's hard to say for certain what the issue is without knowing your project structure but I would expect changing your installed apps to just project_app and your AUTH_USER_MODEL to project_app.CustomerUser should work.
If your app name is "login", you can proceed as follows:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
#apps
'login.apps.LoginConfig']
I think the model in your login application is as follows:
class CustomUser(AbstractBaseUser):
#some fields for customuser
and in settings.py:
AUTH_USER_MODEL = 'user.CustomUser'
AUTH_USER_MODEL = 'app_name.ModelName'
I am trying to install the sites package and upon running makemigrations am receiving the error:
django.contrib.admin.sites.AlreadyRegistered: The model Site is
already registered in app 'sites'.
This is my admin.py:
from django.contrib import admin
# Register your models here.
from django.apps import apps
models = apps.get_models()
for model in models:
try:
admin.site.register(model)
except admin.sites.AlreadyRegistered:
pass
And here are my installed apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webpage',
'django_user_agents',
'analytical',
'corsheaders',
'django.contrib.sites',
]
Any idea what might be causing this issue? Please let me know if there is information missing.
You are registering all models of all apps in your admin.py. You seem to be aware of the fact that some model may already be registered and use a try-except block to catch that. But the problem is that other packages don't know that you are doing this and obviously won't use try-except blocks. You get the error because you successfully register the model Site with the admin site but then when django.contrib.sites tries to register the same it fails since it is already registered.
One solution may to be order your INSTALLED_APPS better and your apps be last. Currently you have django.contrib.sites listed after many apps (even third party ones):
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites', # Move it here
'webpage',
'django_user_agents',
'analytical',
'corsheaders',
]
Again this might cause errors if some other apps try to register their models. Instead of doing this brute force registering of all models consider registering the models for only your app inside it's admin.py:
from django.contrib import admin
# Register your models here.
from django.apps import apps
app_config = apps.get_app_config('your_app_name') # Replace your_app_name it is just a placeholder
models = app_config.get_models()
for model in models:
try:
admin.site.register(model)
except admin.sites.AlreadyRegistered:
pass
Your admin registered the models of sites app before sites app, the best solution is to skip the sites model in your admin so the admin in sites can register its models.
I want extend Application model of django oauth toolkit. They have given intsructions here link
But i am not getting how to do it. I have created one app inside apps folder and inside models i have added the following code.
from django.db import models
from oauth2_provider.models import AbstractApplication
class MyApplication(AbstractApplication):
logo = models.ImageField()
agree = models.BooleanField()
Resgitered the app inside installed_apps as 'apps.oauth2', and added the following line:
OAUTH2_PROVIDER_APPLICATION_MODEL='apps.OAuth2'
But it giving me error
LookupError: No installed app with label 'apps'.
Installed Apps [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'apps.commons',
'apps.company',
'apps.oauth2',
]
According this manual link model in OAUTH2_PROVIDER_APPLICATION_MODEL should be class name not app
My code is as shown below:
class X(models.Model):
class Meta:
db_table = 'X'
ph_no = models.CharField(max_length =255,null=False)
y = models.ForeignKey('y',on_delete=models.CASCADE)
class M(models.Model):
class Meta:
db_table = 'M'
comment = models.CharField(max_length=255,null= False)
x = models.ForeignKey('X',on_delete=models.CASCADE)
class y (models.Model):
class Meta:
db_table = 'y'
details = models.TextField(null= False)
Now when , with this schema, I run the migrations, I get the following error
appName.M.x: (fields.E300) Field defines a relation with model 'X', which is either not installed, or is abstract.
appName.M.x: (fields.E307) The field appName.M.x was declared with a lazy reference to 'appName.X', but app 'appName' doesn't provide model 'X'.
My settings.py file is as follows:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'dashboard'
]
in installed app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
*'dashboard'*
with
'dashboard.apps.DashboardConfig'
]
and apps.py in dashboard application
from django.apps import AppConfig
class DashboardConfig(AppConfig):
name = 'dashboard'
I am trying to customize Products and few other models in the catalogue app following the documentation.
I have forked catalogue app (to myproject/boscar/catalogue) as per documentation documentation and my updated boscar/catalogue/models.py:
from django.db import models
from oscar.apps.catalogue.abstract_models import AbstractProduct
class Product(AbstractProduct):
is_active = models.BooleanField(default=False)
from oscar.apps.catalogue.models import *
I have already included the modified catalogue app, in the INSTALLED_APPS in settings.py as an argument for get_core_apps function.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'django.contrib.sites',
'django.contrib.flatpages',
'bmigrate',
'compressor',
'widget_tweaks',
'boscar'
] + get_core_apps(['boscar.catalogue'])
Migrations are automatically copied to my local app when I executed this command manage.py oscar_fork_app catalogue boscar.
My issue is when I execute the makemigrations command (python "manage.py makemigrations boscar"), it shows "No changes detected in app 'boscar'". But I already made a change to add is_active field in product table.
I believe you need to refer to the catalogue app when migrating:
python manage.py makemigrations catalogue