Django installing custom app(Postgresql model) - django

I've installed following packages https://github.com/zacharyvoase/django-postgres via pip and virtualenv.:
pip install git+https://github.com/zacharyvoase/django-postgres.git
It was installed succesfully. I used it in my model(As described in its documentaion)
from django.db import models
import django_postgres as pg
USStates = pg.Enum('states_of_the_usa', ['AL', 'WY'])
class Address(pg.CompositeType):
line1 = models.CharField(max_length=100)
line2 = models.CharField(max_length=100, blank=True)
city = models.CharField(max_length=100)
zip_code = models.CharField(max_length=10)
state = USStates()
country = models.CharField(max_length=100)
when I try to sync it via shell, it throws an error:
(virtualenv) user$ python manage.py sync_pgviews
Unknown command: 'sync_pgviews'
Type 'manage.py help' for usage.
Have I left something after installing an app? And is it the correct way to install django new app?

In order for management commands to work, the app has to be added to INSTALLED_APPS. However, a basic problem that you have is that the module doesn't support ENUM yet. Its still a work in progress.

After adding new app:
add app to INSTALLED_APPS in settings.py
run python manage.py syncdb
add urls to urls.py
Perhaps you should go through this (again?) https://docs.djangoproject.com/en/dev/intro/tutorial01/

Related

Django deploy errors

I just launched the Django app. As an image, everything is in place, but the form and admin panel do not work. Anyone who knows please help
I get this error when I run the form.
Let me know if I need to share any code.
model.py
from django.db import models
# Create your models here.
class Form(models.Model):
fname_lname = models.CharField(max_length=50, verbose_name="Ad Soyad")
number = models.CharField(max_length=20, verbose_name="Nömrə")
send_date = models.DateTimeField(auto_now_add=True, verbose_name="Tarix")
class Meta:
verbose_name = 'Formlar'
verbose_name_plural = 'Formlar'
def __str__(self):
return self.fname_lname
As Marco suggested, once you deploy you should run your migrations since you're probably using a different database. You run migrations same as when in development depending on your platform the following should work:
python manage.py makemigrations
python manage.py migrate
just make sure you are on the same directory as the manage.py file. Also remember that you will have to tweak the settings.py file database settings if you haven't already.
try the commands below:
python manage.py makemigrations --fake
python manage.py migrate --fake
if it donot work, i think you have to delete database and recreate it,
if you need the data you can use python manage.py dumbdata ->data.json
and after you create the new database use python manage.py loaddata data.json

Problem with migrating my Database using Django

I currently have a database using Heroku and want to migrate it to AWS, where both use PostgresSQL. So, after some digging on how to get it done I followed the steps as on this youtube video.
I initially ran python manage.py dumpdata > dumpdata.json with my Heroku database credentials in Django.
Afterwards, I changed my database credentials in settings.py to the AWS database credentials, and ran python manage.py migrate --run-syncdb which worked successfully.
And then I ran the code python manage.py loaddata dumpdata.json, when where I was thrown an error.
The following error came up:
django.db.utils.IntegrityError: Problem installing fixture 'C:\Users\Acer\Desktop\Web Development\Proffesional\eblossom\eblossom\eblossom\dumpdata.json': Could not load contenttypes.ContentType(pk=9): duplicate key value violates unique constraint "django_content_type_app_label_model_76bd3d3b_uniq"
DETAIL: Key (app_label, model)=(user, profile) already exists.
I don't understand what has gone wrong over here, the site is working perfectly fine all this time with no database compilation error, but now when I try to migrate it to AWS, I am thrown with this problem.
Just in case my models.py:
class Profile (models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
mobile_number = models.CharField(max_length=12, null=True)
guest = models.BooleanField(default=False)
def __str__(self):
return f"{self.user}"
I do this all the time between databases, local and remote, postgres or sqlite.
In order to achieve that, do the following steps in order :
# Start the same as you did on local database
python manage.py dumpdata > db.json
# push this file to your production/server location
# and delete or start with a fresh new database
python manage.py migrate
python manage.py shell
# Enter the following in the shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
# Exit shell and run following command
python manage.py loaddata db.json

Django 3.0.7 - No Changes Detected after making Changes to Model

I am making changes to a model AppContactCnt to add new fields to handle deleting of records.
Here is that models.py
class AppContactCnt(models.Model):
id_cnt = models.AutoField(primary_key=True)
# idcst_cnt = models.IntegerField()
idcst_cnt = models.ForeignKey(AppCustomerCst, models.DO_NOTHING, db_column='idcst_cnt')
idctp_cnt = models.ForeignKey(AppContactTypeCtp, models.DO_NOTHING, db_column='idctp_cnt')
idtrp_cnt = models.IntegerField()
name_cnt = models.CharField(max_length=50)
phone_cnt = models.CharField(max_length=50, blank=True, null=True)
email_cnt = models.CharField(max_length=100, blank=True, null=True)
note_cnt = models.CharField(max_length=100, blank=True, null=True)
receives_emails_cnt = models.BooleanField(blank=True, null=True)
deleted_cnt = models.BooleanField(blank=True, null=True)
# date_deleted_cnt = models.DateTimeField(blank=True, null=True)
# deleted_by_cnt = models.CharField(max_length=100, blank=True, null=True)
class Meta:
db_table = 'app_contact_cnt'
app_label = 'easytrade20'
I tried to add in managed = True no change.
Also tried the following:
Delete all previous migration files and pycache files except init.
python manage.py migrate --fake-initial
python manage.py makemigrations
python manage.py migrate
I am still getting No changes detected
I am not sure how to proceed, any assistance is apprecaited.
You have set app_label = 'easytrade20' in the meta option, but Django is unable to detect an app named easytrade20 in your project. That's why there aren't any changes.
So, add easytrade20 to your INSTALLED_APPS or remove app_label = 'easytrade20' from the model's Meta, and try again to run your migrations.
try this:
python manage.py makemigrations [app_name]
python manage.py migrate
Problem
The app is likely not listed in INSTALLED_APPS located in settings.py, therefore Django has not registered your application and will not detect migrations that need to be run in that application.
Solution
Add easytrade20 to your INSTALLED_APPS and try again to run your migrations.
If the answers above didn't work for you I propose you to make a model and migrations based on an existing database. The steps to complete this task are outlined below.
Below I suggests that your app directory also called easytrade20.
I think that you already have a database that you are working with and all migrations have been applied to it. If not, and saving the
structure of migrations is not critical for you, then skip steps 2-3.
If any of your other apps have migrations that depend on easytrade20 migrations you need to repeat steps 2-5 for every of them (by renaming easytrade20 to your app name) before starting step 6 (and 6+ steps are required for that apps too).
0. Backup your files and database before changes
1. Update your Django version to the latest release
pip install django==3.0.* -U for 3.0.X (3.0.10 is latest release when I write these lines)
or
pip install django==3.1.* for latest one (3.1 have not so much differences from 3.0 and maybe it is right choice for you)
2. Check if all migrations are applied
Use showmigrations command to show applied migrations:
python manage.py showmigrations
You'll see structure with all apps and their migrations like this (app1 and app2 for example):
app1
[X] 0001_initial
app2
[X] 0001_initial
[X] 0002_some_other_migration
You'll see your apps and some django apps/third-party packages, like admin, auth, database, sessions etc.
If you'll see all [X] near every migration - all is fine, go on.
If you'll see some [ ] migration_name - try to apply these migrations first. Run python manage.py migrate app_name migration_name for every unapplied (unchecked) migration top down for every app (app_name for example).
3. Make new models based on your database structure
Use inspectdb to generate models:
python manage.py inspectdb > generated_models.py
File generated_models.py will be created. You need to copy from this file all models that you created for easytrade20 app. Replace your models.py with generated models and remove managed = False at least in the model AppContactCnt.
4. Remove information about easytrade20 migrations from database
python manage.py migrate --fake easytrade20 zero
And now if you'll run python manage.py showmigrations easytrade20 you'll see that all migrations unapplied.
5. Remove your migrations directory
That's it. Remove directory easytrade20/migrations completely.
Then if you'll run python manage.py showmigrations easytrade20 you'll see text (no migrations)
6. Generate new migrations
Run makemigrations command:
python manage.py makemigrations easytrade20
Folder easytrade20/migrations will be created. If not, then you'll need to check two things:
Your settings.py to ensure that easytrade20 included in your INSTALLED_APPS.
Your easytrade20 app settings. Ensure that your files have similar code:
# easytrade20/__init__.py
default_app_config = 'easytrade20.apps.EasyTradeConfig'
# easytrade20/apps.py
from django.apps import AppConfig
class EasyTradeConfig(AppConfig):
name = 'easytrade20'
7. Apply initial migration
If you have database with this table then fake migration:
python manage.py migrate easytrade20 --fake-initial
If not, then run without --fake-initial:
python manage.py migrate easytrade20
8. Make changes to your model, create and apply migrations
Change your models.py file.
Run python manage.py makemigrations easytrade20. New file in your easytrade20/migrations directory will be created.
Run python manage.py migrate easytrade20 to apply new migration.
Make sure your app is registered inside the settings.py, and the INSTALLED_APP section.

python3 manage.py makemigrations No changes detected

(fcdjango_venv) Subinui-MacBook-Pro:Impassion_community subin$ python3 manage.py makemigrations
No changes detected
I'm learning Basic Django right now, and was following the lecture, but got problem.
I followed the lecture, so first I typed the code on models.py
from django.db import models
# Create your models here.
class Impassionuser(models.Model):
username=models.CharField(max_length=64,
verbose_name='사용자명')
password = models.CharField(max_length=64,
verbose_name='비밀번호')
registered_dttm = models.DataTimeField(auto_now_add=True,
verbose_name='등록시간')
class Meta:
db_table = 'Impassion_Impassionuser'
and then on Terminal, I typed
(fcdjango_venv) Subinui-MacBook-Pro:Impassion_community subin$ python3 manage.py makemigrations
but the result was
No changes detected
In the lecture, after typing python3 manage.py makemigrations
it shows
Migrations for "User" :
user/migrations/0001_initial.py
- Create model User
how can I get the same result?
Include the name of your app after the makemigrations command and make sure you have a migrations folder created within your app.

importError: No module named django.db

from django.db import models
# Create your models here.
def Article(models.Model):
title=models.CharField(max_length=200)
body=models.TextField()
pub_date=models.DateTimeField('date published')
likes=models.IntegerField()
def __unicode__(self):
return self.title
This is my code
I am using Django 1.8.2
I am within virtual environment
Are you sure that you create a simple project?
django-admin.py startproject nameofyourproject
mkdir apps
cd apps
django-admin.py startapp nameofyourapp
add in settings in variable "Installed Apps" your new app
nothing should fail...
can you activate your virtual environment. And make sure django is install in your virtual environment by using following command.
pip freeze
out put of pip freeze
(Dev-Env)➜ django_chat git:(singleUserChat) pip freeze
Django==1.7.1