Is there a way not to use the default tables in Django? - django

I am going to make a very simple Restful API. So there is no reason to use the default tables in Django.
auth_group
auth_group_permissions
auth_permission
django_admin_log
etc..
Is it possible to define and use only the model (table) I want without using the default tables provided by Django?

remove the auth and admin application from your INSTALLED_APPLICATION list from your settings.py.
INSTALLED_APPS = [
'django.contrib.admin', # remove this line
'django.contrib.auth', # as well as this
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
And do a fresh migration on the database.
python manage.py makemigrations
python manage.py migrate

Related

How do I reference my model in a nested folder structure to dump data in Django 3.2?

I'm using Django 3.2 and Python 3.9. I have this project directory setup
+ cbapp
- manage.py
- settings.py
+ models
- __init__.py
- crypto_currency.py
In my settings.py file, I have
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cbapp',
]
I want to dump some data to a fixtures file, so I tried
$ python3 manage.py dumpdata cbapp.models.crypto_currency > ./cbapp/fixtures/crypto_currency.json
CommandError: No installed app with label 'cbapp.models.crypto_currency'.
What's the proper way to reference my model to dump data?
Firstly in cbapp/models/__init__.py I think you have to import all models from crypto_currency.py. Like so: from .crypto_currency import *
Then you should be able to use (Replace CryptoModel with the name of your model):
python3 manage.py dumpdata cbapp.CryptoModel > ./cbapp/fixtures/crypto_currency.json

How to Create Django Project without default Django Database table

I'm trying to make Django project but when creating that Django Project There are serval table created in Database which I dont Want. I need Django Project Without any Default Database Table
Database tables are:
auth_group
auth_group_permission
auth_user
django_migration
django_admin_log
django_content_type
I tried Disabling
django.contrib.admin
In Installed Apps
INSTALLED_APPS = [
# 'django.contrib.admin',
# 'django.contrib.auth',
'django.contrib.contenttypes',
# 'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'data.apps.DataConfig',
]
The Expected result should be Django shouldn't create Default table in database
Before the first migration you have to remove these apps from you your INSTALLED_APPS in your settings.py file:
django.contrib.admin,
django.contrib.auth,
django.contrib.contenttypes,
django.contrib.sessions,
but it is not recommended because you will loose many features of django

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

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

How to add another app to an existing Django project?

I deployed my Django project to Heroku. Now, I want to add another app to my existing project. How can I do it in a way that a new database is not created- I mean my existing database should not get deleted?
You can just run heroku run python manage.py syncdb after pushing your new app with model definitions. This won't delete your database. Consider installing South though.
Using PyPI
If the app you want to install is available on the Python Package Index, you can add the name of the add and the version you want to use in your requirements.txt file like:
app_name==1.2.3
Then, in your project/settings.py file, add the app_name as a string to the list of INSTALLED_APPS. For instance, if INSTALLED_APPS looks like this:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
then adding the new app will make it:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'package_name',
)
Using git
You can follow the same steps as above, but instead of adding app_name==1.2.3, you add:
git+git://github.com/owner_name/app_name#egg=app_name
replacing owner_name and app_name appropriately. This clones the repository that you point to, so be sure that it's either public or that you have ssh access if it's private.

Django DatabaseError: relation "django_site"

I currently have a django app am developing on my PC with data in my db but when i try running this app on a test server i get the error below
DatabaseError: relation "django_site" does not exist
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...
can any one tell me why am getting this error please.thanks
You may be calling a site object before creating site model(before syncdb)
ex: site = Site.objects.get(id=settings.SITE_ID)
This issue continues to plague many, including myself. Although a tedious process, this approach saves me the brain power:
Disable all external apps in your INSTALLED_APPS, except your own apps, like so:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'main', # This is my own app.
# 'compressor',
# 'ckeditor',
# 'imagekit',
# 'debug_toolbar',
# 'rest_framework',
# 'allauth',
# 'allauth.account',
# 'allauth.socialaccount',
# 'allauth.socialaccount.providers.google',
# 'allauth.socialaccount.providers.facebook',
)
Run
python manage.py makemigrations
python manage.py migrate
Then, uncomment all the other apps, then repeat makemigrations and migrate above.
That works all the time for me
I can't see your models or what apps are you using, but my guess is that you are using django_site (Site model) and you don't have 'django.contrib.sites' in the INSTALLED_APPS.
If I'm correct then just add 'django.contrib.sites', to your INSTALLED_APPS.
Had this strange issue while initiating a new database and using django-debug-toolbar. Removed it from the INSTALLED_APPS and was able to run syncdb. Then re-added debug_toolbar and it was still working fine.
If you're using django-debug-toolbar, try to comment out debug_toolbar in your installed apps and try again.
Update: Please follow the steps for the explicit setup: http://django-debug-toolbar.readthedocs.org/en/1.2.2/installation.html#explicit-setup
i have same problem and fixed it like this:
add SITE_ID=1 into settings.py
run this command :
python manage.py migrate
not been able to solve this a django way so i tried using sql, i created a dump of just the database like this.
pg_dump mypgdatabase | gzip -c > mypgdatabase.dump.out.gz
then moved it to the server
scp /path/to/mypgdatabase.dump.out.gz my_remote_server
then recreated it on the server like this
psql -d mypgdatabase -f mypgdatabase.dump.out
then run
./manange.py migrate --all
and all when well.
Couple of things you can try and check:
Your settings.py should have a SITE_ID usually = 1
Change order of your INSTALLED_APPS in your settings.py and try temporarily commenting items out.
As Geo said, check that you're not calling a site object before creating site model (ex: site = Site.objects.get(id=settings.SITE_ID)).
Once you got it working, remember to manage.py makemigrations APP_NAME as I found cases where it seem to have then avoided the commenting out step.
I overcame this issue with the following order in INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.sites',
'allauth',
'allauth.account',
# my other apps,
)
My "commenting out" pattern was slightly different than the accepted answers (as shown below). That is to say, it might be unique depending on the values of various dependencies scattered throughout your app. If commenting out the values above throws an error message, I recommend commenting out the apps that throw the error message and uncommenting accordingly until it works for you. This may be a little "brute force" but it should get the job done.
INSTALLED_APPS = [
# django native apps
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
"django.forms",
'django.contrib.gis',
# 'django.contrib.flatpages',
#third party apps
#django-allauth apps
'allauth',
# 'allauth.account',
# 'allauth.socialaccount',
# 'allauth.socialaccount.providers.facebook',
# 'allauth.socialaccount.providers.google',
# 'allauth.socialaccount.providers.twitter',
# 'allauth.socialaccount.providers.github',
# my apps
'app0',
'app1',]
#MIGRATION_MODULES = {"sites": "mysite.contrib.sites.migrations"}
Also make sure SITE_ID = 1 comes before the DATABASE definition in settings.py