Migrating models of dependencies when changing DEFAULT_AUTO_FIELD - django

I'm using Django 3.2. I've changed added this line to settings.py:
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
I then ran these commands:
$ python manage.py makemigrations
$ python manage.py migrate
The makemigrations command creates new migration files for my apps, not just the apps that I have created, but also in my dependencies. For example, I'm using django-allauth, and this file was created in my virtual environment (virtualenv):
.venv/lib/python3.8/site-packages/allauth/account/migrations/0003_auto_20210408_1526.py
This file is not shipped with django-allauth. When I deploy this application from git, this file is not included.
What should I do instead? How can I switch DEFAULT_AUTO_FIELD without the need to create new migration files for dependencies like django-allauth?

Ideally, your third party dependencies would include this line in the config found in apps.py:
from django.apps import AppConfig
class ExampleConfig(AppConfig):
default_auto_field = 'django.db.models.AutoField'
While waiting for upstream dependencies to update their apps.py or migration files, you can override the app config yourself. If it doesn't exist already, create an apps.py file in your main app directory (eg: project/apps.py), and override the config of a dependency. In this example, I'm overriding the config of django-allauth:
from allauth.account.apps import AccountConfig
from allauth.socialaccount.apps import SocialAccountConfig
class ModifiedAccountConfig(AccountConfig):
default_auto_field = 'django.db.models.AutoField'
class ModifiedSocialAccountConfig(SocialAccountConfig):
default_auto_field = 'django.db.models.AutoField'
Then modify INSTALLED_APPS in settings.py to look like this, replacing the old entries for django-allauth in this example:
INSTALLED_APPS = [
# ....
# replace: "allauth.account", with
"projectname.apps.ModifiedAccountConfig",
# replace: "allauth.socialaccount", with
"projectname.apps.ModifiedSocialAccountConfig",
]
If the dependency doesn't have an apps.py file to override, you can still create an AppConfig sub-class in project/apps.py like this:
from django.apps import AppConfig
class ModifiedExampleDependencyConfig(AppConfig):
name = 'exampledependency' # the python module
default_auto_field = 'django.db.models.AutoField'
Now when you run python manage.py makemigrations, no migration files should be created for the dependencies.

I work on a big project, we upgraded Django from 2.2. to 3.2 and then have got a need to create all new models with Big Integer (Int8) (PostgreSQL) field instead of default Integer (Int4).
When I defined it in settings.py:
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
I got the same problem, but with own apps - Django tried to make me to migrate 135 models I had, but I didn't want to do it. I only wanted to create new models with BigInt and manipuate olds manually.
I found the next solution. I changed the field to custom:
DEFAULT_AUTO_FIELD = 'project.db.models.CustomBigAutoField'
And then overrided its deconstruction:
from django.db import models
class CustomBigAutoField(models.BigAutoField):
"""Int8 field that is applied only for new models."""
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
if getattr(self, 'model', None):
path = 'django.db.models.AutoField'
return name, path, args, kwargs
As I discovered, fields of new models don't have a back reference to their models, so path wouldn't be overridden for them.
We override path because Django checks whether a model is changed by a key, that includes the path to this field. So we deceive Django and it thinks that existing model didn't changed.
I might not see the whole picture, but I tested it with different existing and new models and it worked for me. If someone tells me why this solution is bad, I'd be grateful.

Related

Rename Django auth db tables in 3.1.2

I am setting up a new django project and
I would like to rename the three auth tables to include a django_ prefix.
i.e. rename to:
django_auth_group / django_auth_group_permissions / django_auth_permission.
I tried putting the following in __init__.py at the project root from this answer:
from django.contrib.sessions.models import Session
Session._meta.db_table = "django_auth_group"
but was getting the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I'm not sure this is even correct. I think I should be overwriting the model somewhere in my models directory so I tried:
from django.contrib.auth.backends import ModelBackend
class AuthGroup(ModelBackend):
class Meta:
swappable = 'AUTH_GROUP'
db_table = 'django_auth_group'
but this also doesn't seem to create the new table when I re-run my docker container.
Any help would be appreciated. Thanks

Impossible to use django-allauth when there is already an `account` app?

My Django application already has an app called account. Does it mean that it is ABSOLUTELY impossible to use django all-auth because of the name conflict? Due to the existing data, the app account cannot be renamed.
settings.py:
INSTALLED_APPS = [
...
'account',
...
# For allauth:
'django.contrib.sites',
'allauth',
'allauth.account', # Name conflict
...
If so, is there a good alternative?
2-14
Per solarissmoke's suggestion. Where should I put the new app and what is it name?
Is it something like this (Of course, it is wrong)?
my_project/account/apps.py:
import allauth.account
from django.apps import AppConfig
class AccountConfig(AppConfig):
name = 'account'
class AllAuthAccountConfig(allauth.account):
name = 'allauth.account'
label = 'allauth_account' # Change this
verbose_name = 'aullauth_account'
This is a known problem with django-allauth.
You can work around it by changing your own app to use a different app label. In your app's AppConfig:
class AccountConfig(AppConfig):
name = 'my_project.apps.account'
label = 'my_project_account' # Change this
verbose_name = 'account'
And refer to this app config in your INSTALLED_APPS, e.g.,
INSTALLED_APPS = [
...
'account.apps.AccountConfig',
...
'allauth',
'allauth.account',
...
Which should now work because the app labels are unique. Note that the only issue with this is that database tables names for your account app will have to change so as not to conflict with the allauth app - this will require some data migrations (if on an established project) or creation of fresh migrations (if on a project where you can afford to clobber the database).
You can also do this with the allauth.account app if that's easier - just create a new app config anywhere in your project, e.g.,
my_project/allauth_apps/apps.py (make sure to also create __init__.py in this new directory):
class AllAuthAccountConfig(allauth.account):
name = 'allauth.account'
label = 'allauth_account' # Change this
verbose_name = 'aullauth_account'
And then in your INSTALLED_APPS replace account with my_project.allauth_apps.apps.AllAuthAccountConfig. As above, this changes the database table names.
you need to fork the git on your own and change the label
fork the source code from https://github.com/pennersr/django-allauth/
add unique label such as allauthaccount to app in django-allauth/allauth/account/apps.py on your own forked git
commit
add following line to your requirements.txt -e git+https://github.com/andylee0213/django-allauth#egg=django_allauth
do "pip install -r requirements.txt and pip freeze > requirements.txt" and check github link is still in requirements.txt
but my suggestion is, instead of not receiving updates and going through all this pain, just use other auth libraries. There are many other libraries that does not depend on all auth. check
https://medium.com/codex/django-allauth-vs-dj-rest-auth-vs-python-social-auth-vs-drf-social-oauth2-ef7d50f92d16

Django app_label is wrong on models

I have a Django DRF application. Here is my project structure.
myproject/
myproject/
apps/
myApp1/
__init__.py
apps.py
admin.py
models.py
urls.py
views.py
myApp2/
__init__.py
static/
manage.py
and myINSTALLED_APPS contains:
INSTALLED_APPS = [
'apps.myApp1.apps.AppOneConfig',
'apps.myApp2.apps.AppTwoConfig',
]
When I went to ./manage.py shell_plus and run:
SomeModel._meta.label
I see myApp1 or myApp2 instead of apps.myApp1 && apps.myApp2. And even in migrations Models are referred as myApp1.Model or myApp2.Model not as apps.myApp1.Model or apps.myApp2.Model
Also, specified AppConfig.
from django.apps import AppConfig
class AppOneConfig(AppConfig):
name = 'apps.myApp1'
verbose_name = 'My App One'
Is that expected ? I am pretty new to Django. Can anyone suggest what the mistake was?
Is that expected?
Yes, that is expected. By default, the app label uses the last part of the "python path". You can change it by specifying this in the AppConfig [Django-doc]. It is the .label attribute [Django-doc] of this AppConfig that determines the app label, and:
(…) It defaults to the last component of name. It should be a valid Python identifier. (…)
Now the .name attribute [Django-doc], and this is:
Full Python path to the application, e.g. 'django.contrib.admin'.
You can specify this by first specifying the AppConfig in the __init__.py file of your myApp1 directory:
# apps/myApp/__init__.py
default_app_config = 'apps.myApp.apps.App1Config'
then you make a file apps.py in the myApp1 directory, and write:
# apps/myApp/apps.py
from django.apps import AppConfig
class App1Config(AppConfig):
label = 'apps_myapp1'
Note: normally directories use slug_case, so I think it might be better to rename your myApp1 to myapp1 or my_app1.
EDIT: You thus need to set the label attribute of your AppOneConfig to:
class AppOneConfig(AppConfig):
name = 'apps.myApp1'
label = 'apps_myapp1'
verbose_name = 'My App One'

Django Settings Apps Model

I want to know what is the meaning of "Config" in installed_apps in django like this
'polls.apps.pollsConfig'
Is it okay to put only the name of the apps example 'sample_apps'?
In the subfolder polls you have a file apps.py with a class named PollsConfig
polls/apps.py
from django.apps import AppConfig
class PollsConfig(AppConfig):
name = 'polls'
verbose_name = 'PollsApp'
This file is autogenerated by django when you create a new class but you can modify it to add some proprieties.

Add a field to Mezzanine blogpost

I am using Mezzanine for a project. I need to add a extra field to Mezzanine blogpost.
I notice using EXTRA_MODEL_FIELDS can do it, but it looks complex.
I also try copy the blog folder from the site-package to my project path, and then modify the models.py. but I doesn't work.
I am new to Django, can some one help?
Thanks
By do some research, now I got the answer:
1. copy the blog app from sites-package to my project
2. change my setting.py
INSTALLED_APPS = (
"blog", #it was "mezzanine.blog",
.....
3. modify the blog/models.py
add following line to class BlogPost
shop_url= models.CharField(max_length=250,null=True, blank=True)
4. migirate the table (installed South)
./manage.py schemamigration blog --auto
./manage.py migrate blog
You can create a django app (CustomBlog), add it to your installed apps
and remove or comment the Mezzanine blog:
INSTALLED_APPS = (
"CustomBlog", #it was "mezzanine.blog",
...
)
In the models.py and admin.py, of your CustomBlog, inherit from the class BlogPost of Mezzanine:
models.py
from django.db import models
from mezzanine.blog.models import BlogPost
from mezzanine.blog.models import BlogCategory
class CustomBlog(BlogPost):
# Add New Field
# example
new_field = models.CharField(max_length=255)
class CustomBlogCategory(BlogCategory):
pass
admin.py
from django.contrib import admin
from .models import CustomBlog,CustomBlogCategory
admin.site.register(CustomBlog)
admin.site.register(CustomBlogCategory)
Then in the terminal create and run migrations
python manage.py makemigrations
python manage.py migrate