I created a web app and deployed on Heroku successfully even database migrations. When I open the app I see the error like this:
'something here' matching query does not exist.
App URL: https://lp7.herokuapp.com/lp7/
App isn't working and if I remove this data feild from model, then app works but no single data is coming from database. But, when I go to heroku database it shows:
No. of Tables = 28
No. Rows = 220
Size of data = 9.4Mb
It means, the all migrations exists on heroku but not showing on website.
Any solution..?
You need to update the table for model Topbar in Heroku. You can use admin-site to update it.
Also, for future, you might want to change from:
num = TopBar.objects.get()
to
num = TopBar.objects.last()
So it will return the last object created in queryset. It will return None if no object has been created for TopBar
Looking at the traceback https://lp7.herokuapp.com/lp7/ it is bad here
num = TopBar.objects.get() you should pass something matching
if you dont need it do it like
try:
num = TopBar.objects.get(id=1)
except TopBar.DoesNotExist:
pass
#handle if not found logic here
For migrating data from your local database to heroku database is to run:
python manage.py dumpdata yourapp > yourapp/fixtures/app_data.json
Then you need to commit this file to heroku branch, for example:
git commit heroku main
After commiting run the following command to load data into heroku database:
heroku run python manage.py loaddata app_data
Related
I have a background task in my django application, which enters all the rows from a csv into one of the tables in my database. I pass the csv via the admin site, that creates a background task, that I can run with python manage.py process_tasks. This all works locally, but on my heroku app, for some reason it doesn't.
I thought maybe inputting data is impossible from the heroku console, but if I run python manage.py shell on the heroku console, I can input data just fine.
This is the code that inputs the data into the database:
from background_task import background
...
#background(schedule=5)
def save_course_from_df(df):
df = pandas.read_json(df)
db = 0
for index, row in df.iterrows():
print("%s percent done!" % str(db / df.shape[0]))
db += 1
values = dict(row)
values = {key: values[key] for key in values.keys()
if type(values[key]) != float or not math.isnan(values[key])}
try:
Course.objects.update_or_create(
url=row['url'],
defaults=values
)
except IntegrityError:
pass
print('done!')
I run this by opening the heroku console and running 'python manage.py process_tasks'. I get the print messages, and no error is being thrown. Still, my database doesn't change.
I expected that after the task runs I would have a full table. Instead, nothing changed.
It seems the problem was with my migrations. For some reason the database on the heroku app had a not null constraint for a column whose model had the null=True argument.
(I am aware that a number of Django users have had the same issue.
I have looked at a number of solutions online but none has worked for me so far.)
I have set up my apps.py, settings.py and models.py files as explained in Django official tutorial (please see the 3 files below).
When I enter in the terminal:
$ python3 manage.py makemigrations munichliving_app
It returns:
No changes detected in app 'munichliving_app'
(file settings.py) in INSTALLED_APP --> I added and tested both one at a time:
'munichliving_app' and
'munichliving_app.apps.MunichLivingConfig'
apps.py file: https://pastebin.com/raw/qaYy1x44
setting.py file: https://pastebin.com/raw/cSsbfPsx
models.py: https://pastebin.com/raw/U0QeM16k
Django official tutorial states that I should see something along the lines of:
Migrations for 'polls':
polls/migrations/0001_initial.py:
- Create model Choice
- Create model Question
- Add field question to choice
Thank you.
Your app is munichliving (the module that contains models.py), but you have munichliving_app in your INSTALLED_APPS setting. The munichlivin_app is the project folder (the one that contains settings.py). It doesn't normally contain models so you shouldn't usually have to add it to INSTALLED_APPS or make migrations for it.
Replace 'munichliving_app' with 'munichliving' in your INSTALLED_APPS.
Next, I would remove your apps.py because it doesn't appear to be used. If you do keep it, then change it to name='munichliving', then use'munichliving.apps.MunichLivingConfig'inINSTALLED_APPS`.
Finally, create migrations with
./manage.py makemigrations munichliving
Try this:
python manage.py migrate --fake appname
Or delete the migration folder in your app, go to the database and delete the file in django_migrations table, then migrate again:
python manage.py makemigrations
python manage.py migrate
I'm trying to work with django_cities_light and have followed the docs to a T and also referenced some other SO questions but no data is being imported.
When I open a python shell and do
from cities_light.models import City
c1 = City.objects.get(id=100)
c1 returns
cities_light.models.City.DoesNotExist
I've ran
./manage.py migrate
./manage.py cities_light
But there is still no data.
settings.py
CITIES_LIGHT_TRANSLATION_LANGUAGES = ['en']
CITIES_LIGHT_INCLUDE_COUNTRIES = ['FR']
CITIES_LIGHT_INCLUDE_CITY_TYPES = ['PPL', 'PPLA', 'PPLA2', 'PPLA3', 'PPLA4', 'PPLC', 'PPLF', 'PPLG', 'PPLL', 'PPLR', 'PPLS', 'STLMT',
Does anyone have an idea of how I can fix this?
think i the way:
1.rollback all migration for the app
./manage.py migrate cities_light zero
next aplpy it again
./manage.py migrate
and try to do force import
./manage.py cities_light --force-import-all
may be the last command can be enough
Done as follows but no column is added.
Migrate database
python manage.py db migrate
Edit migrations/versions/{version}_.py
def upgrade():
from alembic import op
op.add_column('table_name', Column('column_name', INTEGER) )
Update schema
python manage.py db upgrade
The reason is that alembic stores version in table called alembic_version, and once {version} is in alembic_version, then nothing happens.
The solution is to create a new migration script and do migrate again.
Instead of repeatedly deleting my tables, recreating them and populating with data in my dev env, I decided to create a bash script called reset_db that does this for me. I got it to whack the tables, recreate them. But it's not able to populated the tables with data from the django orm.
I try to do this by calling the django shell from the script and then running ORM commands to populate my tables. But it seems like the django shell commands are not running.
I tried running the django orm commands manually/directly in the shell and they run fine but not from within the bash script.
The errors I get are:
NameError: name 'User' is not defined
NameError: name 'u1' is not defined
NameError: name 'm' is not defined
Here is my script:
#!/bin/bash
set +e
RUN_ON_MYDB="psql -X -U user --set ON_ERROR_STOP=on --set AUTOCOMMIT=off rcamp1"
$RUN_ON_MYDB <<SQL # Whack tables
DROP TABLE rcamp_merchant CASCADE;
DROP TABLE rcamp_customer CASCADE;
DROP TABLE rcamp_point CASCADE;
DROP TABLE rcamp_order CASCADE;
DROP TABLE rcamp_custmetric CASCADE;
DROP TABLE rcamp_ordermetric CASCADE;
commit;
SQL
python manage.py syncdb # Recreate tables
python manage.py shell <<ORM # Start django shell. Problem starts here.
from rcamp.models import Customer, Merchant, Order, Point, CustMetric, OrderMetric
u1 = User.objects.filter(pk=5)
m = Merchant(u1, full_name="Bill Gates")
m
ORM
I'm new to both django and shell scripting. Thanks for your help.
You should look at creating a fixture to populate your db https://docs.djangoproject.com/en/dev/howto/initial-data/
You need to import User explicitly. The django package and a few other things are automatically imported, but not everything you might want.
Also, to avoid not know what to import, there are management commands. This will leverage your Django and Python. You can learn shell scripting later.
clearly seen in your mistakes is not recognized as a model class User django-admin maybe you lack some import or something like this
from django.db import models
User import from django.contrib.auth.models
, by the way In line
User.objects.filter u1 = (pk = 5)
I think I put
u1 = User.objects.filter (pk = 5). First ()
at the end.
Anyway, here I leave some threads that may be of help,
https://docs.djangoproject.com/en/dev/ref/django-admin/
http://www.stackoverflow.com/questions/6197256/django-user-model-fields-at-adminmodel
https://groups.google.com/forum/?fromgroups = #! topic/django-users/WrVp1DDFrX8
Hope this helps.