ValueError in Django when running the "python manage.py migrate" command - django

I needed to add more fields to Django's User model, so I created a custom model class (named Accounts in an app named accounts) that extends Django's AbstractUser class.
After that, I updated my settings.py file, defining the AUTH_USER_MODEL property:
AUTH_USER_MODEL = 'accounts.Accounts'
I then created a migration file for the custom model using the python manage.py makemigrations command.
After that, I ran the python manage.py migrate command and I got this error message:
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.accounts', but app 'accounts' isn't installed.
What's the cause of the error and how can I fix it?
UPDATE:
Now, if i run the python manage.py makemigrations command, I get this error message:
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.accounts', but app 'accounts' doesn't provide model 'accounts'.

You just delete your previous 0001_initial.py in the migrations folder and try doing the makemigration and migrate again

I have the similar problem. It is the admin app has the cache and migrations history. I solve it by deleting all the cache and migrations history record(pycache file, and 0001.intial etc., keep init.py only) in
YouProject\lib\site-packages\django\contrib\admin\migrations

I too had a similar problem when I changed the name of one of my apps, I had to delete migrations files at two locations, all migrations for the specific app migration folder, then migrations at "Your-project-env/lib/python3.5/site-packages/django/contrib/admin/migrations".

You didn't add accounts to your INSTALLED_APPS. From the comment, I can see accounts.apps.AccountsConfig in your list of apps. Instead of it, just add accounts to your INSTALLED_APPS

It's just because you have already an instance of default user model I think. Start a new project and migrate your models again and it should work.

I have the same problem.
look under your class in models.py, change the app_label to the name of your application, I put the name of the class, and then the error came. doing this I think it fixes the errors.
class Meta:
app_label = 'put_app_name'
Sorry for some grammar error, I'm using google translator.

Related

How to add site models to Cookiecutter Django project

I have a Cookiecutter Django project in which I'd like to add a "SiteSettings" model to control constants. My first instinct was to run manage.py startapp site. However I received the following message:
CommandError: 'site' conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name.
Upon closer inspection, it appears that Cookiecutter already created a sites module within contrib, but with no models.py file. So I decided to make a models.py with the SiteSettings model in this directory.
However when I run makemigrations it says: No changes detected in app 'sites'
How can I add my desired model to this module and get migrations working for this module?
You will run into countless problems if you follow this path. Just use another name and do manage.py startapp newname.

Django makemigrations No changes detected in app

I have trouble with my makemigrations command.
Note: I have successfully make migrations till now, so it is not the first time I try to make migrations on this project.
I have my project in INSTALLED_APPS.
Problem: For some reason project stop detecting any changes in my models.
Inside my project models.py I have:
from myproject.myfolder import myModel1
from myproject.myfolder import myModel2
from myproject.myfolder import myModel3
if a add new models as myModel4 class and import it inside models.py and I try to
python mamange.py makemigrations environment=local
I get No changes detected
I know there are a lot of posts of making the initial migrations, so I even try
python manage.py makemigrations myproject environment=local
I even try to delete all files in __pycache__ but it doesn't work for me.
I even try to delete database and create new one, and it doesn't work either.
EDIT:
Because I delete the database and make it new again, database is empty, but I still get same message.
I just ran into an issue like this. In my case, the problem was that I had installed, through pip, the stable version of the package that I was developing, and Django was importing the stable version rather than my development version. To check if this is the case with you, try adding a syntax error to models.py. If makemigrations doesn't trigger the syntax error, then you'll know that your version is not even being loaded by the python interpreter.
If your model is not inheriting from django model then, you will see aforementioned error. Make sure that your custom model inherits from django models.Model, something like this.
from django.db import models
class Posts(models.Model):
...
Deleting the DB and creating new one will never work since it refer the previous migration files. Delete all previous migration files and pycache files except init. Then try running these.
python manage.py migrate --fake-initial
python manage.py makemigrations
python manage.py migrate
This worked for me

Creating users in django (NOT NULL constraint failed: auth_user.last_login)

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

Django 1.7 - create initial migration

According to the Django docs, if I want to create an initial migration for an app, I should do:
$ python manage.py makemigrations my_app
However, if I do that in my project, I get:
No changes detected in app 'my_app'
even though there are no migrations for my_app yet - the my_app/migrations/ folder only has an __init__.py file.
I do NOT have managed = False in my model. The model classes in question don't even have a Meta class defined. What else can prevent Django from detecting model changes?
How does Django detect if/when there are changes?
Update:
I should add that migrations for this particular app worked fine back when I was using South migrations. It's only after upgrading to Django 1.7, and built-in migrations, that it can no longer figure out if/when there are model changes for that particular app (migrations for other apps work fine).
A little late, but having just hit this after creating a brand new app, it suddenly dawned on me that I hadn't added the new app to the INSTALLED_APPS in the settings.py.
INSTALLED_APPS = (
...
my_app,
...
)
Doing that and then re-running python manage.py makemigrations my_app generated the initial migration.
You might want to look for a "migrations" directory somewhere in your virtualenv home directory or on your path.
I ran a few times into some similar issue, when trying to migrate an app from South to Django 1.7 migrations. For some reason, Django wouldn't find the correct migrations folder, and so would create the migration into an unlikely location such as <virtualenv>/bin/myapp/migrations dir (when using django-admin.py). So everytime I'd run makemigrations Django would find this "stale" migration, and display the No changes detected in app 'my_app' message.
Sorry if I'm vague on the specifics, I'll update next time I run into this issue.

How to remove models from django?

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