django oscar model customization : model change is not reflected while makemigrations - django

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

Related

Django error with sites package: "The model Site is already registered in app 'sites'"

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.

ValueError - How to Extend OAuth2 Application Model - Django

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

Django not creating table for custom user

I've been trying to create a custom user to store extra fields in Django, but after specifying the new User and deleting the old database, Django does not want to make a table or any migrations for my app "accounts"
Error (when doing anything user related e.g. logging in):
django.db.utils.OperationalError: no such table: accounts_user
Auth User Model in settings.py:
AUTH_USER_MODEL = 'accounts.User'
Installed apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts',
'forum',
]
accounts/models.py:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
Edit:
I apologize, I forgot to mention. I have run makemigrations and migrate.
Makemigrations returns "No changes detected." Migrate does everything but any models existing in my accounts/models.py
Turns out I had to specifically run "makemigrations accounts". Not sure why, but it worked.

Django installed_apps does not recognise my app?

This is my installed apps section in settings.py.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'homepage.apps.HomepageConfig',
.
.
.
]
My app name is "homepage" and I really can't remember why I changed it to 'homepage.apps.HomepageConfig', but whatever it is, it worked on my machine.
Now, i uploaded my files to server, installed required apps, did the migrations, but i noticed django does not create my "homepage" app table and does not migrate anything from my app. And my website returns the error: table homepage_post does not exist.
What is wrong?
Check if in homepage app directory You have file named __init__.py and apps.py. The content of apps.py should be:
from django.apps import AppConfig
class HomepageConfig(AppConfig):
name = 'homepage'

Django 1.10 - Plug new app into django

I have a trouble with plugging recently created app called pages into a django project.
My pages apps.py
from django.apps import AppConfig
class PagesConfig(AppConfig):
name = 'pages'
My settings.py, installed apps:
INSTALLED_APPS = [
'pages.apps.PagesConfig' # pages.apps.pages fails too
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
ImportError: No module named 'pages.apps.PagesConfigdjango';
'pages.apps' is not a package
Does anyonw knows what is wrong there? I did everything according the docs, but django still cannot to plug my app. Any insights appreciated.
You are missing a comma after your entry.
'pages.apps.PagesConfig',