Dump data from django Feincms - python

I'm using feincms in a django project and I want to use manage.py dumpdata but I get nothing:
python manage.py dumpdata feincms
[]

If you want to dump the page data you need to run dumpdata on the page app. The page models live there, not in feincms:
python manage.py dumpdata page

I don't know FeinCMS, but looking at the GitHub repo it seems that the feincms application only contains abstract models. If you want to dump the data, you'll need to find where the actual concrete models are, and dump that app.

There is also the option to use dumpdata all, but I should warn you: the more complex your application, the more likely it is that the dumpdata will miss something. If all else fails, try doing a dump directly from your database, rather than relying on Django.

Related

Copy Django App to a New Project but Model Data is Lost

I made a development django project on my pc, and I added a bunch of data for one of the apps "things" by logging in the ADMIN panel. When I copy and paste this app folder into a new project on Ubuntu, add this app in setting, then I find all the data in the model is gone. I used "makemigrations, migrate". Still nothing. What should I do to have those data in this new project?
Based on the comments it seems that some kind of a solution has already been found. Here I'd like to give my 2 cents.
Django offers command line tools for exporting and importing data. With
python manage.py dumpdata app_label > backup.json
you can export all the data for the app app_label and save it into a file backup.json.
Later you can use this file to load the data:
python manage.py loaddata backup.json
There are many other options, please check the official documentation for further info, like for example using XML instead of JSON and much more.
EDIT:
If you search for dumpdata in the questions tagged with django here at SO, you'll get many hundreds results.

Django : export table in file SQL

I try to deploy my website developped with Django 1.9. I use the web hosting AlwaysData.
The problem is that the database of my web hosting is empty (no tables) and I want to import my tables (in models.py) in this database.
For that, I need a file sql that contains all my tables. After I will import this file in my AlwaysData database.
I try the command dumpdata but the format sql doesn't exist (only yaml, xml and json).
How can I do ?
I use PostGreSQL.
Thank you
I would think that you could build your database using ./manage.py makemigrations and then ./manage.py migrate. However, if that does not work for you, it sounds like you want the sqlmigrate management command:
./manage.py sqlmigrate my_app my_migration > my_migration.sql
This will generate the SQL for your migration, and redirect it to a file called my_migration.sql.

How to install custom SQL with Django

What's the best way to deploy custom SQL for views used by unmanaged Django models?
I have a model myapp.models.MyModel using a view myview.sql, specified in the models "db_table" meta attribute. So, following the docs, I placed the custom SQL file in myapp/sql/myview.sql.
However, after I run python manage.py syncdb, the view is not installed. Running sqlcustom similarly does not show the view.
What else do I need to do so Django will automatically detect and deploy my custom SQL?
python manage.py sqlcustom will only output the custom SQL statements that you've defined, but you need to execute them in the database. Of course, you can copy and paste, but if you want to automate the process and have it be less tedious/error-prone, one easy way to do so (at least on a *nix environment) is to use a pipe:
python manage.py sqlcustom myapp | python manage.py dbshell
According to the doc, name the file myapp/sql/mymodel.sql instead of myapp/sql/myview.sql

django syncdb and an updated model

I have recently updated my model, added a BooleanField to it however when I do python manage.py syncdb, it doesn't add the new field to the database for the model. How can I fix this ?
From Django 1.7 onwards
Django has built in support for migrations - take a look at the documentation.
For Django 1.6 and earlier
Django doesn't support migrations out of the box. There is a pluggable app for Django that does exactly that though, and it works great. It's called South.
Django currently does not do this automatically. Your options are:
Drop the table from the database, then recreate it in new form using syncdb.
Print out SQL for the database using python manage.py sql (appname), find the added line for the field and add it manually using alter table SQL command. (This will also allow you to choose values of the field for your current records.)
Use South (per Dominic's answer).
Follow these steps:
Export your data to a fixture using the dumpdata management command
Drop the table
Run syncdb
Reload your data from the fixture using the loaddata management command
As suggested in top answer, I tried using South, and after an hour of frustration with obscure migration errors decided to go with Django Evolution instead.
I think it's easier to get started with than South, and it worked perfectly the first time I typed ./manage.py evolve --hint --execute, so I'm happy with it.
Havent used django in a while, but i seem to remember that syncdb does perform alter commands on db tables. you have to drop the table then run again and it will create again.
edit: sorry does NOT perform alter.
In django 1.6
At first we have run - python manage.py sql <app name>
Then we have to run - python manage.py syncdb
If you run Django with Apache and MySQL, restart apache after making migration with makemigrations.

update django database to reflect changes in existing models

I've already defined a model and created its associated database via manager.py syncdb. Now that I've added some fields to the model, I tried syncdb again, but no output appears. Upon trying to access these new fields from my templates, I get a "No Such Column" exception, leading me to believe that syncdb didn't actually update the database. What's the right command here?
As of Django 1.7+, built-in migrations support, allows for database schema migrations that preserve data. That's probably a better approach than the solution below.
Another option, not requiring additional apps, is to use the built in manage.py functions to export your data, clear the database and restore the exported data.
The methods below will update the database tables for your app, but will completely destroy any data that existed in those tables. If the changes you made to your app model do not break your old schema (for instance, you added a new, optional field) you can simply dump the data before and reload it afterwards, like so:
Django 1.4.15 and earlier
python manage.py dumpdata <your_app> > temp_data.json
python manage.py reset <your_app>
python manage.py loaddata temp_data.json
Django 1.5 and newer
python manage.py dumpdata <your_app> > temp_data.json
python manage.py sqlclear <your_app> | python manage.py dbshell
python manage.py syncdb
python manage.py loaddata temp_data.json
(The reset command was deprecated and then removed in Django 1.5)
If your changes break your old schema this won't work - in which case tools like South or Django Evolution are great.
As of Django 1.7, you can now do this with native migrations. Just run
python manage.py makemigrations <your app name>
python manage.py migrate
Seems like what you need is a migration system. South is really nice, working great, has some automation tools to ease your workflow. And has a great tutorial.
note: syncdb can't update your existing tables. Sometimes it's impossible to decide what to do automagicly - that's why south scripts are this great.
Django's syncdb doesn't alter existing tables in the database so you have to do it manually. The way I always do it is:
Change the model class first.
Then run: manage.py sql myapp.
Look at the sql it prints out and see how it represented the change you are going to make.
Make the change manually using your database manager.
Check to see if everything worked correctly using the admin site.
If you are using sqllite a good manager is the firefox plugin: link
Another tool would be django evolution. No table dropping needed in most cases.
django evolution
Just install it as any other django app and run:
python manage.py evolve --hint --execute
deseb is a great tool for that.
Having it installed, you can write ./manage.py sqlevolve and it'll generate sql commands necessary to keep the database structure in sync with your models.
You need to drop your tables before you can recreate them with syncdb.
If you want to preserve your existing data, then you need to unload your database,
drop your tables, run syncdb to build a new database, then reload your old data into your new tables.
There are tools that help with this. However, in many cases, it's just as easy to do it manually.
For versions 1.4.1 and above users the command has changed to
python manage.py flush
Please read the official document before using it as it will delete all your data.

Categories