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

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

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.

drop/reset one/multiple (specified) models from database with django

I've added a field to my django model. Now, since I've had some instances of this model already, they not include this field resulting in django error page. I'm not able to manually delete those instances from admin panel.
I've searched that python manage.py flush is a way to go for reseting whole db, but I would like to delete only this particular model.
Is there a way to do this from the CLI ?
Solution:
For starting question, I accept #IainShelvington answer:
python manage.py shell
# then
from app_name.models import model_name
model_name.objects.all().delete()
Additionally, thanks to #AbdulAzizBarkat for providing:
python manage.py migrate <app_name> zero

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

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.

Reset Django registration models?

I am trying to implement django registration-redux 1.2. I installed the application and added to it settings.py of my project. I ran manage.py syncdb as well as makemigrations/migrate. Typing these commands again and I get no changes detected. However it seems like the tables are not getting created. When I try to register I get the following error:
ProgrammingError at /main/register/ (1146, "Table 'la_test_serve.registration_registrationprofile' doesn't exist")
Is there a way to reset the project/app so that these tables get created?
Thanks,
Robert
Try to run schemamigration for your registration apps
python manage.py schemamigration registration --initial
after that run migrate
python manage.py migrate registration
Remove the app from your "INSTALLED APPS" setting. Then run manage.py makemigrations and manage.py migrate. Reinstall the app.
Note: If you didn't add 'registration' (yes, simply 'registration') to your "INSTALLED APPS", it won't work.
I found this error occurred when I was reinstalling django-registration-redux.
Either way, check that you have deleted not only the table for registration in the database but also ensure that in the migrations table delete the corresponding row, in this case 'registration'.

Django - accidently removed superuser

I am using Django 1.4.5 and accidentally deleted the superuser. How can I recreate the superuser account for an existing project? I found tutorials that show to do it when initially setting up a project but can't figure out how to create a superuser for an existing project.
I did try django-admin.py createsuperuser in the terminal without any luck
I apprecite the time and feedback.
You can invoke this command through manage.py
./manage.py createsuperuser