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
Related
I am a new in django. I storage global application variables inside settings environment folder. But now i need store variables inside Database because i want changes them from django admin.
My way to do this: Create model class in django core, where define two variable app_name - list of application and data-JSON. But i think this is not best praxis. Since it may be difficult to use the GIT
Decided to use a ready-made solution: django-extra-setting.
In apps setting define variables that will be storage in DB:
DB_VARIABLES = {'variable_name': ['text', 'default_value']}
Reads a variable from files after django project has been loaded
from settings import PROJECT_APPS
from extra_settings.models import Setting
from importlib import import_module
class CoreConfig(AppConfig):
def ready(self):
if (is_manage_py and is_runserver) or (not is_manage_py):
local_settings = import_module(f'path/{app_name}')
for app_name in PROJECT_APPS:
local_constants = local_settings.DB_VARIABLES if hasattr(local_settings, 'DB_VARIABLES') else False
if local_constants:
variables = {**variables, **{key: local_constants[key] for key in local_constants}}
for constant_name in local_constants:
if Setting.objects.filter(name=constant_name).count() == 0:
constant_values = local_constants[constant_name]
Setting(name=constant_name, value_type=constant_values[0], value=constant_values[1]).save()
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.
I have a django project with multiple apps. In one of the apps when I add custom permissions to any model and run makemigration, the migration-file to add the permission is created. When I apply the migration I get no error messages but the permission isn't added to the auth_permission table.
class Meta:
app_label = 'my_app'
permissions = (
('assign_work_type', 'Assign work type'),
)
The migration completes without errors
I have tried doing the same in other apps and that works. I have also tried adding a column to the current app and that works as well. Anyone got any idea what it could be? I am running django 1.11.26
UPDATE
Here is the content of the migration file
# -*- coding: utf-8 -*-
# Generated by Django 1.11.26 on 2019-11-25 11:13
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('timereport', '0143_auto_20191122_1754'),
]
operations = [
migrations.AlterModelOptions(
name='worktype',
options={'permissions': (('assign_work_type', 'Assign work type'),)},
),
]
After quite some investigation I found that the affected app was missing the models_module, i.e. the "models.py" file. I have all my models in a /model/ directory and a while back I deleted the models.py file thinking it was of no use.
Adding the models.py file back solved the issue
I've been following the DjangoProject tutorial. When I run python manage.py startapp newapp while in the same directory as manage.py. In the newapp directory I see init.py, models.py, tests.py, and views.py but not admin.py file. Where is admin.py?
I am running Django 1.5.1 in Windows 8
You have to create an admin.py file.
you don't necessarily need an admin.py file,
just import the admin module in your models.py file,
from django.contrib import admin
and for each model do the following:
admin.site.register(model1)
admin.site.register(model2)
However, this is not best practice, but since it's just a tutorial, it will work.
You also need to uncoment the relevant lines in the urls.py file
I think I had the same frustrations following the DjangoProject tutorial - however, when I cross-referenced it with with the DjangoBook tutorial (for the same version, I believe, 1.5.1), I found that an admin.py file was not necessarily created after a python manage.py startapp xyz command -- moreover, I also uncommented all of the admin options in urls.py, views.py, and settings.py - so a bit of a mix of what Neal and Ibrahim said
You have to create your own admin.py file in the app if you want it. Indeed, this file is optionnal and isn't created by startapp.
If you want a default template to begin your admin.py, it should be:
from django.contrib import admin
from models import Model1, Model2
class Model2Admin(admin.ModelAdmin):
list_display = ('title', 'content', 'date')
# Just an example, chekc docs and tutorials for more info.
admin.site.register(Model1)
admin.site.register(Model2, Model2Admin)
The reason there is no default admin.py is because you don't have any models yet when you create your new application; so there is nothing to add to the admin section.
Further, you may not want to admin all the models in your application; or you may be creating an application that does not need any admin hookups; or you may not be using the admin application at all in your project.
Since django cannot decide this for you, there is no default admin.py generated.
To create one, if you are following the tutorial - simply keep reading and in part two you'll create the admin.py file when you learn about the admin contrib app and how to integrate it with your custom models.
I have been given a small legacy db (mysql) to work with, and therefore, I have tried to use the inspectdb command in django.
So, I use something like:
python manage.py inspectdb > models.py
It works fine, and I take the models.py and copy the class it created into my app (searchapp) as a models.py file.
I have done a syncdb, and everything went fine. Results are as expected.
Now, I have tried to add this model into my admin site, using:
from searchapp.models import Abaqus
from django.contrib import admin
admin.site.register(Abaqus)
stored in a file called admin.py (Abaqus is the name of the class generated by inspectdb and searchapp is the app name). However, I am not able to see the app on the admin site. I have checked my settings.py and it includes the 'searchapp'.
I was wondering if someone could point me in the right direction..
I suspect admin.py is not loaded. You could check this by putting a debug statement just above the register call:
import pdb;pdb.set_trace()
admin.site.register(Abaqus)
If this is in fact the case, the correct way to ensure admin.py is loaded is to call django.contrib.admin.autodiscover() at the beginning of your main url conf.
If you've written no admin classes and don't want an admin.py, you can call admin.site.register(Abaqus) right below where the model is defined, inside models.py.
If you have an admin module structured like the following, import admin within models.py to ensure the code is run:
myapp
|--models.py
|--views.py
|--admin
|--__init__.py
|--views.py
|--base.py
Another possible cause would be that you are missing the permissions to edit the model. Double check that you are logged in as a superuser.