Forums

Cannot save a new user with django admin

Hi,

I am deploying a django application, with django 1.9 and python 3.5. I have enabled the admin interface, but I get an error when I try to save a new user.

In models.py, I have:

from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=100)
    title = models.CharField(max_length=3)
    birth_date = models.DateField(blank=True, null=True)

In admin.py, I have:

from django.contrib import admin
from django.contrib.auth.models import User, Group
from .models import Author
admin.site.register(Author)

I did python3.5 manage.py migrate and python3.5 manage.py makemigrations. I can create an new Author, and save it via the admin interface, but I am unable to do it with a new user.

Any suggestion ? Thanks.

The error.log gives :

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 541, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/sites.py", line 244, in inner
return view(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/lib/python3.5/contextlib.py", line 30, in inner
return func(*args, **kwds)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/admin.py", line 128, in add_view
extra_context)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 1437, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/lib/python3.5/contextlib.py", line 30, in inner
return func(*args, **kwds)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 1378, in changeform_view
self.save_model(request, new_object, form, not add)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 991, in save_model
obj.save()
File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/base_user.py", line 74, in save
super(AbstractBaseUser, self).save(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 700, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 728, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 812, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 851, in _do_insert
using=using, raw=raw)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1060, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 323, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: auth_user.last_login

Did you run migrate and then make_migrations, or the other way round? You need to run make_migrations first, so that migrate has something to do.

Thank you for the suggestion.

I think I had a broken db.sqlite3, because I first ran python manage.py syncdb, implicitely calling python2, and then python3.5 migrate, etc... So I removed db.sqlite3, created a new db.sqlite3. Afterwards, I ran python3.5 manage.py makemigrations , then python3.5 manage.py migrate, and now everything is OK.

I'm having the same issue, but how do I run python manage.py migrate inside of my virtualenv?

I added the virtualenv after the fact so I'm wondering if that is part of the issue? New to Django and virtualenv...

Just click on the "Start a bash console in this virtualenv" link below the place where you specified the virtualenv on the "Web" tab. Then you can run the command from there.