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

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.

Related

Dynamic site elements not displaying after Django database change

So I recently changed databases on a Django-based website from SQLite to MySQL.
I did it via dumping the data from the SQLite into a json file and then migrating it to the new MySQL db then loaded it using the python manage.py loaddata command.
The site is hosted on pythonanywhere and the tranistion there went flawless, however, on the local copy of the project I have(used to test implementing new features and debug), the dynamic elements on the front page wont load.
I have a dynamic js table with information for various cryptocurrencies.
This is how the head of the table looks on the live site:
And this is how it looks on the local copy of my machine, with my own hosted MySQL server:
What I've tried:
-Collectstatic
-Running ContentType.objects.all().delete() in python manage.py shell
I also did research on stackoverflow but I could not find a similar issue.
I am also new to Django so it might be something obvious I'm missing
Try with Django fixtures.
Here is the official link for Django fixtures
Solved: I used the datadump.json from pythonanywhere rather than my local copy. Also had some issues with the json utf8 encodings so make sure your datadump file is utf8 encoded and/or matches the encoding of your db.

Want to run a python script on a django application

I'm working on a project that takes a web url and prints a summary of the text contents of the web page. I've written a program that does that in python and now I want to make it a web application so I decided to try django.
I've been reading the official tutorial(I'm not done, I've only gotten up to Models) but when I try to apply what I've learned and actually make the application I find myself lost.
"Where do I actually put the python code that will run on the backend?" I'm not using a database so I don't think it should be in the models.py file. Do I import it in views.py? Should I even be using django? I'm beginning to feel like it's overkill.
As you probably have seen in the tutorial, a Django project usually have several apps. Each app typically has models.py, views.py, admin.py, etc. Where to store the script depends on your project structure:
if only one app needs it, just put the script under the app
if there're several apps need the script, I mostly like will start an app called "common" or "utils", and put it there
if the script is used in multiple projects, and updated actively, I will consider make it a standalone Python package. And install it in the project's virtualenv
And where to import the script, also depends:
if the app is not complicated, no other dependency, views.py is the place to go
if the app is quite complicated that you even need to separate them into multiple views, I may create a common.py under the app to import the script
About Django or not, it depends our your (potential) project complexity:
if you project may grow big or you will use Python to write big web project, Django is worth to learn, as it's the most powerful web framework in Python
if you only need a simple web application that even doesn't need DB, Flask is simpler to learn, as Paulo said

Modules not found in django

I want to make an application in django with two apps named apps and data.the "data" apps is placed within "apps".I had entered 'apps.data' in the Installed apps in "settings.py".when I run the devserver i got this error "no modules named apps.data".Any one please help me.
what is the reason for nesting "data" app in "apps"? it is uncommon to nest one app inside of another when there are only two apps (unless you have some great reason to). suggestions:
create an apps app, and create a data model for it;
create an apps app, and a data app; (link them however through the models)
the answer to your problem is probably file structure, but your basic requirements for what you're asking is detailed in your app requirements.
Long story short.. what application are you trying to create?

Dump data from django Feincms

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.

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