Django app not showing registered models in Admin page - django

I have tried everything I can think of and find on here but nothing seems to be able to get my models to register and show on my Django Admin page. Weird thing is I have another model that is already appearing on the Admin page that I've matched the syntax for but that one works and this other one does not.
models.py
class SimpleModel(models.Model):
something = models.CharField('Simple Model', max_length=200, default='foobar')
def __str__(self):
return self.name
admin.py
from .models import SimpleModel
admin.site.register(SimpleModel)
settings.py
INSTALLED_APPS = [
'bootstrap4',
'app.apps.RequestsDashboardConfig',
'applications_dashboard',
'requests_dashboard',
#'applications_dashboard.apps.ApplicationsDashboardConfig',
'requests_dashboard.apps.RequestsDashboardConfig',
#"app.requests_dashboard",
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
You can see I've even tried multiple variations of the INSTALLED_APPS but even this still does not work. I don't even get any errors when I start the server with this series of commands after deleting the migrations.
python3 manage.py flush
python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py createsuperuser
python3 manage.py runserver 0.0.0.0:80
I even tried deleting the entire DB, recreating it, and then running this again and still no luck!

Not sure where I went wrong in the end but for anyone who encounters similar issues to mine, I was able to solve this by doing another complete DB rebuild.

Related

Error after adding Django channels to installed_apps in settings file

I am just trying out django channels so I created a virtual environment and installed django, drf and channels. It threw error asking for visual c++ build tools which got resolved after installing it. Then I created a channels project and an app. Then just for testing I added a sample model as below and registered it with the admin. It compiled well and also I was able to see the model in the admin page.
My Model Calss
from django.db import models
# Create your models here.
class College(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=150)
objects = models.Manager()
def __str__(self):
return self.name
My admin.py
from django.contrib import admin
from .models import College
# Register your models here.
admin.site.register(College)
Now the Problem
I added channels to the INSTALLED_APPS list in the settings.py file like below,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'channelApp',
'channels',
]
Now when I try to run the server using the runserver command I get the following error
ModuleNotFoundError: No module named 'win32api'
LookupError: No installed app with label 'admin'.
I have been searching but failed to find any suitable answer. Kindly help me out.
Thanks in advance.
Just after I posted this I stumbled onto a SO post
Issue after installing django channels
Just to reiterate in short, this is an open bug and the work around is to install the following package
pip install pypiwin32
After installing you may have to close and reopen the editor for the changes to reflect. And the error is resolved.

Pyforms web sample now working, only django's default site showing up

I'm trying to follow along with the example shown here: https://pyforms-web.readthedocs.io/en/v4/getting-started/first-app.html to get the first app running.
The Directory Structure of the default Django app looks like that.
I added the code as in the example, ran migrate and opened the brower, but only the default Django page shows up.
My site_crawl.py looks like this:
from pyforms.basewidget import BaseWidget
from confapp import conf
class SiteCrawlApp(BaseWidget):
UID = 'site-crawl-app'
TITLE = 'Site crawl'
LAYOUT_POSITION = conf.ORQUESTRA_HOME
ORQUESTRA_MENU = 'left'
ORQUESTRA_MENU_ICON = 'browser'
ORQUESTRA_MENU_ORDER = 0
The settings.py looks like:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'project',
]
Those are the only changes I made to the default structure created when I ran django-admin startproject project also ran python manage.py migrate before starting the web server with python manage.py runserver.
I'm using Python 3.6.0, Django 2.1.7 and Pyforms v4.
Can somebody help me figure out what I'm doing wrong? Thanks!

Custom user model with Django CMS. Cannot resolve bases for cms.PageUser

I'm trying to use custom user model with Django CMS. I created new users app with this model:
users.models:
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
Here project settings:
settings:
INSTALLED_APPS = [
'djangocms_admin_style',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'django.contrib.messages',
'users',
'cms',
'menus',
...
]
AUTH_USER_MODEL = 'users.User'
Why I have this error?
manage.py makemigrations users
django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [<ModelState: 'cms.PageUser'>]
This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
in an app with no migrations; see https://docs.djangoproject.com/en/1.8/topics/migrations/#dependencies for more
I ran into same problem and I followed your instruction but on step 9 I encounter this error:
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency users.0001_initial on database 'default'.
I got it to work by reordering your step (without commenting out the AUTH_USER_MODEL and without commenting out users from installed apps)
Removed migrations folder from users app
Started with a blank database
Ran manage.py makemigrations users
Ran manage.py migrate
Ran manage.py makemigrations
Ran manage.py migrate
I ran into the same issue. Based on a reply to https://github.com/divio/django-cms/issues/3436 I did the following which worked for me:
Removed migrations folder from users app
Commented out users from installed apps
Commented out the AUTH_USER_MODEL bit
Started with a blank database
Ran manage.py makemigrations
Ran manage.py migrate
Put back the things, I commented out previously.
Ran manage.py makemigrations users
Ran manage.py migrate
I know it's an old post but it might help others.

RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

I am building an application with Django Rest Framework and AngularJs. I am using Django-rest-auth for my authentication purposes, although, I have not been able to set it up. Anyway, I am trying to set up this app with my project. I realized I need to install django-rest-auth-registration to get it running, so I followed this documentation to do the following things:
I ran the commands
pip install django-rest-auth
and
pip install django-allauth
Any my settings.py looks like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 3rd party apps
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'allauth',
'allauth.account',
'rest_auth.registration',
# My app
'myapp',
]
I have also added the authentication backends, context_processors, and the proper urls.
However, when I try to migrate, my terminal throws the following error:
RuntimeError: Model class django.contrib.sites.models.Site doesn't
declare an explicit app_label and isn't in an application in
INSTALLED_APPS.
Why do I get this error, and how do I solve it to migrate my project? Thanks!
The fix
Just add Django's Sites framework to your apps and set SITE_ID to 1 in your settings.
INSTALLED_APPS = [
...
'django.contrib.sites',
]
SITE_ID = 1
Why does this happen?
Django's Sites Framework is a contributed module bundled with the core library that allows for the use of a single Django application/codebase with different sites (that can use different databases, logic in views, etc). The SITE_ID setting, as stated in the docs, "is used so that application data can hook into specific sites and a single database can manage content for multiple sites."
In this particular case AllAuth requires the Sites Framework in order to function properly. Many other third-party libraries are built to safely handle cases where multiple sites may be present and as such may be best .
I landed on this post via Google search. My problem was running tests that blew up with the error:
RuntimeError: Model class app.taxonomy.models.Term doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
This was running on Python 2.7.x with absolute imports. As mentioned by Colton Hicks in the comments, below, this can also happen with Python 3 (pytest 3.2.3 with Django 1.11.4).
In my tests.py:
from __future__ import absolute_import
[...]
from .models import Demographics, Term
After changing the relative import to an absolute import the problem went away:
from taxonomy.models import Demographics, Term
HTH
Try adding the app_label = 'yourApp' in the models Meta class:
class Meta:
app_label = 'yourApp'
I got the error above. However my problem was the in the urls.py. I was following PyDanny cookiecutter django recipe. My error was to put in the urls.py this line:
url(r'^demo/', include('project.demoapp.urls', namespace='demoapp')),
when I corrected to this:
url(r'^demo/', include('demoapp.urls', namespace='demoapp')),
all was well. I also changed my local apps (I did this first and so the critical error was the url misconfiguration):
LOCAL_APPS = [
# Your stuff: custom apps go here
'demoapp.apps.DemoAppConfig',
]
I have django debug toolbar installed and this was actually causing the/my problem.
INSTALLED_APPS (in settings.py) needs the entry 'django.contrib.sessions'. Make sure to run migrate after adding.
Just add 'django.contrib.sites', to INSTALLED_APPS and set SITE_ID = 1 in your settings.py file.
Upgraded Answer for Django>=4.0 // 2022
Add Django's Sites framework and FlatPages Framework to your INSTALLED_APPS and set SITE_ID in your settings.
INSTALLED_APPS = [
...
'django.contrib.sites',
'django.contrib.flatpages',
]
SITE_ID = 1
Your tests should work like a charm
This error occurred because I had created a new app folder for a subset of sites related to another feature. This needed to be added to my INSTALLED_APPS in settings.py
Django 4.1+ (2023)
After almost an hour digging, what solved for me was this:
INSTALLED_APPS = [
...
'django.contrib.sessions',
]
No need for SITE_ID or additional INSTALLED_APPS entries.
Everything worked as expected after I made a migration
python manage.py migrate
Good luck

nothing happens when running "python manage.py sql polls"

I am trying to get Django running on Google App Engine using Django non-rel. I am following the Django 1.5 tutorial However, when i run:
python manage.py sql polls
nothing is returned. Can anyone suggest why 'manage.py sql' is silently failing?
When I sync the database I get:
$ python manage.py syncdb
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
The database is specified in settings.py as follows:
# Activate django-dbindexer for the default database
DATABASES['default'] = {'ENGINE': 'dbindexer', 'TARGET': DATABASES['default']}
AUTOLOAD_SITECONF = 'indexes'
and I have remembered to include 'polls' in the settings.py
INSTALLED_APPS = (
# 'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.auth',
'django.contrib.sessions',
'djangotoolbox',
'autoload',
'dbindexer',
'polls',
# djangoappengine should come last, so it can override a few manage.py commands
'djangoappengine',
)
and the models are present in polls/models.py
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
NOTE: If I change settings.py to use a local sqlite3 database, 'manage.py sql polls' behaves as described in the tutorial. Therefore, as far as I can tell, this has nothing to do with:
the /polls directory structure
the /polls/models.py file
the python path
Why do you expect it do anything? GAE is, specifically, a non-relational (NoSQL) datastore. There is quite simply no SQL to produce.
You should be aware that GAE, even with django-non-rel, is quite different from standard Django, and following the Django tutorial is only likely to confuse you.