I'm new django web framework. I have a models.py file where i given information about my entity fields, i ran the manage.py migrate command table generated i have alter the table manually without touch the models.py later I have run the python manage.py inspectdb > myapp/models.py in controller level using os.system models.py updated then i have tried models.objects.all() I'm unable to get the newly added field, I have called the view By using ajax call , after page refresh only new field name coming.
can you help how to get all field names with in the ajax response only with out page refresh. It would be grateful if you can help me
Note: I have checked in view level only with out page refresh first time only new column not came after page refresh it came
Thanks in advance...
I'm looking for cache in django
Before migrating, you need to create migrations.
If your DB is correctly configured, here you are (python3/python depending on version that you are using):
First: python3 manage.py makemigrations
Second: python3 manage.py migrate
makemigrations creates SQL queries, and migrate runs it. You can read more here: Django 1.8 - what's the difference between migrate and makemigrations?
I wanna use sorl.thumbnail django app for generating thumbnails in my site.
I followed the guide and I added sorl.thumbnail to INSTALLED_APPS.
Also, I want to use the default key-value store, so I need to use the migrate command, but this doesn't do anything, it doesn't create the required database table. I tried the following commands, but it still does not create the tables:
python manage.py makemigrations
python manage.py makemigrations sorl.thumbnail
python manage.py migrate
Whats going on? How can I migrate and add the required table in order to use default key-value store?
If I dont migrate the db it raises me a exception that says that the table thumbnail_kvstore wasn't found.
I'm coding a web app with django and now I'm starting to handle users.
I'm trying to do the easy part, just create a new user throught admin interface, but when I try to do it I get a error and I don't find any info about it.
I enter django admin, log in with superuser, go to users, add user.
It only asks for: username and password.
When I submit changes then I get a NOT NULL constraint failed, this:
NOT NULL constraint failed: auth_user.last_login
I revised django documentation, and there it says to create a user like this:
from django.contrib.auth.models import User
user = User.objects.create_user('john', 'lennon#thebeatles.com', 'johnpassword')
So, I open a terminal, and do "./manage.py shell" "import django" "import User" and then I try this code, but fails too, it says:
NameError: name 'User' is not defined
Maybe I've changed something in the first part of the project? I've created models, templates, urls and used /static/ for references to files.
Thanks for your help!
last_login field changed in django 1.8. Just run the migrations
python manage.py migrate
Same issue, Had to run python manage.py migrate --fake and then python manage.py migrate and it worked perfectly.
In the app folder there is migration folder.. remove all files from it just keep pycache folder and init.py
then execute python manage.py migrate and then python manage.py makemigrations ..
simply we are removing the older migrations and migrating them again
Every time I wipe the database of my Django app during testing (or when cloning or deployin), I have to go into /admin and set up permissions and groups. Where would I put the code that would populate the DB with them and what would it look like?
For this you can use fixtures.
For example:
python manage.py dumpdata auth > fixtures/auth.json
This will store all models of package 'auth' (Users, Groups Relations) into auth.json
After Deployment you can use the following command to load:
python manage.py loaddata auth fixtures/auth.json
This will restore your prev state of 'auth'.
Maybe it's good for you to switch to South, a very famous part of Django to migrate databases instead of recreating them.
You can provide fixtures with the initial required data and it will be automatically inserted when you syncdb. See docs
In Django how do you remove models that you have synced into the database?
For example in Django tutorial page had this following code
from django.db import models
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
Then I used python manage.py sql polls and python manage.py sql choice to create the tables into the database. But what if I did something wrong and don't want that model any more. What's the syntax to remove it?
If you don't want to delete and re-sync your current database, the best way is to drop the table from your model manually:
$ python manage.py dbshell
> (Inside the DB shell)
> DROP TABLE {app-name}_{model-name};
Why not simply try deleting the models from your models.py file? When you run
python manage.py makemigrations
the migrations file should be updated with the deleted models.
There is no syntax. Django doesn't removes tables or columns. You have to manually change your database or use a migration tool like South.
If you justing playing around with tutorial the easier way is to delete your sqlite database file and run a sync again.
Commenting out the class that defines the model did it for me. Once I had done it and ran python manage.py makemigrations,
I got this as response:
- Delete model MyModel.
Checked afterwards with a DB Browser and it was actually removed.
The most easiest solution is to just delete your model from models.py and run
python3 manage.py makemigrations
(Note: Remove the model from everywhere where you have imported it like admin.py, views.py, or any other file where you have imported it)
If you are facing issue to update changes onto DB so you can directly run this command.
python manage.py migrate --run-syncdb
I found a simpler method, by sheer experimentation. Even after deleting tables, Django was not making the migrations, so I did the following:
Simply delete the files created in your myapp->migrations directory, making sure that you do not delete the init.py and pycache
Starting from 001initial.py and downwards delete the files.
Run python manage.py makemigrations
Run python manage.py migrate
-M
Django’s database handling through syncdb is purely additive: any new models will be added, but deleted models will not be deleted and modified models will not be modified.
If you do not have any data you want to preserve, you are fine just dropping and recreating the database: if you have anything you want to preserve, or even if you intend to have anything you want to preserve, I cannot advise you strongly enough to use a migration tool: South has been the de facto standard for every project I’ve worked on.
Since the Migration command handle Model(database) you can do following steps.
First type
python manage.py makemigrations app_name # it will restructure your model
then type
python manage.py migrate app_name # it will apply to restructure your database.
Example:
I had Posts and PostDetail model,
later on, I wanted to remove PostDetail model and some fields(columns) from Posts model too.
I simply run migrations and migrate commands,checked in Mysql Database. It worked fine.
Hope it will work for you too.
Weather you’re removing a single model or a full app, you should first
remove the desired models from the models.py file.
Moreover, you have to make sure that no other file imports these
models or uses them (admin.py, views.py, etc).
Next, run South or migrate your database to properly delete these
models from your database.
Check the source of this information on the link below:
http://www.marinamele.com/how-to-correctly-remove-a-model-or-app-in-django