This drives me mad... I'm reorganizing an existing Django project using the following structure:
[project_abc]
[app]
[core]
[app1]
admin.py
models.py
...
[app2]
admin.py
models.py
...
... etc ... there's a total of 9 apps
[rest]
... rest api stuff, non-db related ...
[mobile]
... mobile stuff, non-db related ...
[
south
tastypie
[project_abc]
settings.py
urls.py
manage.py
All apps with models that require database access have been added to settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'[app].[core].[app1]',
'[app].[core].[app2]',
...
'tastypie',
'south'
)
Each model class has a Meta class like:
class Meta:
app_label=[app] # this points to the top level above [core]
Directories [app], [core] and subsequent [app] directories have an __init__.py file in them.
When I run syncdb, it happily ignores my apps under [core] however the tables for other apps like tastypie and south get created properly.
When I run manage.py validate it returns 0 errors found
I've read probably all posts and hints on topics related to syncdb but unfortunately to no avail. I'm obviously missing something, but cannot figure out what it is....
I can't fully understand which [app] is set in models Meta, but note that django syncdb uses django.db.models.get_apps to find projects' applications. Latter interspects apps from INSTALLED_APPS, and explicetely tries to load apps' models module with
models = import_module('.models', app_name)
So applications outside INSTALLED_APPS won't have tables synced.
Second, django loads all the models with django.db.models.get_apps for each found application, and latter turn introspects AppCache.apps_models (that cache is, as far as I remember, populated with register_models by model constructor). So all the imported models corresponding to valid applications are processed.
I guess you have to ensure that [app] from models._Meta:
contains models.py (possibly empty) which will make it a django application;
is mentioned in INSTALLED_APPS, to be asseccible with get_apps function.
Related
I have a Django project with an application called application.
The app is installed in the INSTALLED_APPS as follows:
### application/settings.py
INSTALLED_APPS = [
...
'application.apps.MyAppConfig',
]
with the AppConfig:
### application/apps.py
class MyAppConfig(AppConfig):
name = 'application'
verbose_name = 'My App'
label = 'application'
path = f"{os.environ.get('APP_DIR')}/application"
default = True
I have the models defined like this:
### data/models/basemodel.py
from django.db import models
class MyBaseModel(models.Model):
# ... fields ...
Meta:
app_label: `application`
Project's tree:
main_dir/
data/
__init__.py
models/
__init__.py
basemodel.py
application/
__init__.py
apps.py
urls.py
settings.py
...
Django is not finding the models, and if I run makemigrations Django responds No changes detected, and on migrate, the app application does not appear on the Operations to perform.
I want to split the data layer from the logic and interface layers. In order to do so, I've the models and other data-related business inside data (outside any installed app)
The goal is to link the models to the app from there, without having to declare any model inside the application dir.
The Model should be detected so the Django detects migrations to apply.
Context:
Django Application reference: doc
Django Models reference: doc
I guess you want to have a single module per model ? If so, you need to import your models inside models\__init__ like so:
from .basemodel import BaseModel
I also suggest that you define __all__ inside your init file, in order to import your models easily in other parts of the app:
__all__ = ["BaseModel"]
from .basemodel import BaseModel
I'm working with Django 3.2 and trying to configure correctly the AppsConfig subclass of apps.py in order to avoid duplicate apps names when initzialing the project.
The context is the following. I have in my INSTALLED_APPS two apps whose names are equal although their paths not:
INSTALLED_APPS = [
...
'first_app.myapp',
'second_app.myapp'
]
To avoid the error shown below (and according to the documentation), I need to create an apps.py file subclassing AppConfig in at least one of the apps called myapp. I've decided to create that file in the second one, second_app.myapp.
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: myapp
The app.py in second_app.myapp module looks like as follows:
from django.apps import AppConfig
class MySecondAppConfig(AppConfig):
name = "second_app.myapp"
label = "my_second_app"
And in the __init__.py I've added:
default_app_config = 'second_app.myapp.apps.MySecondAppConfig'
My admin.py looks like:
from django.contrib import admin
from .models import MyModel
class MySecondAppAdminModel(admin.ModelAdmin):
list_display = ('attr_1', 'attr_2')
admin.site.register(MyModel, MySecondAppAdminModel)
When I start the project all works OK and I can use that model information, views called from second_app.myapp also work OK.
The problem comes when I access to the admin site (http://localhost:8000/admin), where only appears the first_app.myapp admin form instead of both.
Can you help me? Thanks in advance.
instead of this:
INSTALLED_APPS = [
...
'first_app.myapp',
'second_app.myapp'
]
Just try this way only:
INSTALLED_APPS = [
...
'first_app',
'second_app'
]
You got that error because here first_app.myapp and second_app.myapp myapp is duplicate. you should not use like this in both the apps.
OR
INSTALLED_APPS = [
...
'first_app.apps.MyFirstAppConfig',
'second_app.apps.MySecondAppConfig'
]
When creating an app with python manage.py startapp myapp, it automatically creates an apps.py file.
from django.apps import AppConfig
class MyappConfig(AppConfig):
name = 'myapp'
When I removed it, everything seems to work as before (at least my tests all passed). Is it a bad practice to remove these kind of files from the apps? Should we keep them to avoid side effects?
The recommended approach in Django is to use the app config in your INSTALLED_APPS:
INSTALLED_APPS = [
'myapp.apps.MyappConfig',
...
]
If you do this, then the apps.py file is required.
However, if you do not customize the app config at all, then it is possible to remove the apps.py if you use the app name in INSTALLED_APPS instead:
INSTALLED_APPS = [
'myapp',
...
]
I would like to create a User model that has an additional couple fields in django. I tried to do so by following the advice
https://stackoverflow.com/a/22696794/3426600 to create a custom user model
in models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
user_data = models.TextField(null=True)
In settings.py
AUTH_USER_MODEL = 'app_name.User'
but when I run python manage.py syncdb
I get the errors:
CommandError: One or more models did not validate:
account.emailaddress: 'user' has a relation with model app_name.User, which has either not been installed or is abstract.
admin.logentry: 'user' has a relation with model app_name.User, which has either not been installed or is abstract.
auth.user: Model has been swapped out for 'app_name.User' which has not been installed or is abstract.
socialaccount.socialaccount: 'user' has a relation with model app_name.User, which has either not been installed or is abstract.
Could I be using the wrong app_name? If so, where do I find the app_name?
How do I make the custom user work with socialaccount, etc?
Assume your project strucutre is something like below, and the codes of User(AbstractUser) are located in mysite/mysite/app1/models.py:
mysite/ (root folder for the project, put it anywhere in your disk. Most developers have a workspace folder(s) in computer)
(other non-application things. E.g. a static/, README.md, ...)
manage.py
mysite/ (container for all applications. You can put applications directly under root folder, but not recommended)
__init__.py
settings.py
urls.py
wsgi.py
app1/ (each application resides in its own folder. You don't want to put all models in one models.py file or all views in one view.py file, right?)
__init__.py
models.py
views.py
......
Then the app_name is app1, i.e. AUTH_USER_MODEL = 'app1.User'. Meanwhile, you need to add mysite.app1 into INSTALLED_APPS in settings.py. This probably will solve you CommandError issue.
ADDED NOTES:
Each application, you can consider it as a module of your project. The app_name of the application is the folder name. If you have defined models in one application, must add it into INSTALLED_APPS in settings.py.
Manually creating a folder is not a good way. django-admin.py startapp is more recommended, because it together creates some common files for an application, e.g. init.py, models.py, view.py, ...
Suggest you to go through the django quick-start guide, if you haven't done it: https://docs.djangoproject.com/en/dev/intro/tutorial01/
I'm building a Django app and now I'm in production. I have this problem: after performing manage.py syncdb (all it's ok) I go into admin and I can not find the models tables . My admin.py file is present and this is my file url.py:
from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'stambol.views.home', name='home'),
# url(r'^stambol/', include('stambol.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
Where is the problem?
Permissions
First off, make sure you have permission to edit the missing models. It's common to be developing with a superuser account, and then to test your production deployment with a different non-superuser account. If you don't have at least read permission, the class won't be listed at all in the admin.
I think this is the most likely cause, but I will leave the rest since I had already written it.
Discovering your admin registrations:
One notable difference between runserver and a production server is that when you run runserver, it imports all your models.py files and validates the models. This does not happen in production, so if you register your models with the admin inside models.py you need to be sure to import that file so that code runs. You could do so in your main url conf.
The preferable solution is to do your registration in per-app admin.py files so they are picked up by autodiscover.
Settings:
You do need admin listed in installed apps, as #Pratik says. It also has some dependencies, as mentioned here. Installed apps should contain at least this:
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions'
'django.contrib.messages',
'django.contrib.admin',
#...
'myapp',
#...
]
Make sure the dir containing your app myapp is in your python path, so that myapp is picked up by autodiscover. This is working correctly for you already, or else you would get something like ImportError: No module named myapp.
Restarting the server:
Finally, just to recap what is buried deep within comments, you can restart your production server after making any code changes by touching your wsgi file: touch wsgi.py. Use tab-completion when you do this to be sure you're touching the existing wsgi file and not creating a new one thanks to a typo or some such. The wsgi file you're touching should contain something like this:
...
# tell django to find settings at APPS_DIR/mainsite/settings.py'
os.environ['DJANGO_SETTINGS_MODULE'] = 'mainsite.settings'
# hand off to the wsgi application
application = WSGIHandler()
Still broken?
If things still aren't working as expected, think farther outside the box. Keeping in mind that you're new to your production environment, is it possible some other code besides your own is being served up? Make some obvious change to a front-end page, restart the server, and see if it works. This is just a shot in the dark, of course.
from django import admin
from example.models import YourModel
admin.site.register(YourModel)
# or
class ModelAdmin(admin.ModelAdmin):
pass
admin.site.register(YourModel, YourModelAdmin)
This should do the trick
Make sure you have the below removed as comments inside INSTALLED_APPS in settings.py. Then run ./manage.py syncdb again. They should like as shown below without the # in front of them
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',