python - Django models.DateTimeField django.db.utils.IntegrityError: NOT NULL constraint failed -


what problem? have googled , have not found thing gets close have. saving profile continue next page produces error.

i working on development server using sqlite.

thank you.

models.py

class schoolprofile(models.model):     create_date = models.datetimefield(         null=false, blank=false, **auto_now_add=true,**         verbose_name=u'fecha de creación')     modification_date = models.datetimefield(         null=false, blank=false, **auto_now=true,**         verbose_name=u'fecha de última modificación')     cycle = models.charfield(         null=false, blank=false, max_length=9,         verbose_name='ciclo escolar')     name = models.charfield(         null=false, blank=false, max_length=100,         verbose_name='nombre de la escuela')      def __unicode__(self):         return '{} {}'.format(self.cycle, self.name)      class meta:         db_table = 'perfil escolar'         ordering = ['id', 'name']         verbose_name = 'perfil escolar'         verbose_name_plural = 'perfiles escolares'      class admin:         pass   class schoolservices(models.model):      school_profile = models.foreignkey(         schoolprofile,         on_delete=models.cascade)     tipo = models.integerfield(         choices=school_type_choices, default=school_type_kinder,         verbose_name='tipo de escuela')     folio = models.charfield(         null=true, blank=true, max_length=40,         verbose_name='folio')      def __unicode__(self):         return '{} [{}]'.format(self.tipo, self.folio)      class meta:         db_table = 'servicio escolar'         ordering = ['tipo', 'folio']         verbose_name = 'servicio escolar'         verbose_name_plural = 'servicios escolares'      class admin:         pass 

forms.py

class schoolprofileform(modelform):     class meta:         model = schoolprofile         readonly_fields = (             'create_date',             'modification_date',)         fields = (             'cycle',             'name',)   class schoolservicesform(modelform):     class meta:         model = schoolservices         fields = (             'tipo',             'folio',) 

views.py

class schoolprofileview(loginrequiredmixin, createview):     model = schoolprofile     form_class = schoolprofileform     context_object_name = 'school'     template_name = 'profile/school_profile.html'      def get_success_url(self, **kwargs):         return reverse(             'profile_edit',             kwargs={'pk': self.object.id}) 

urls.py

urlpatterns = [     url(r'^create$',         views.schoolprofileview.as_view(),         name='profile_create'), ] 

using 'admin' interface can fill create form, after submit 'save' following error:

internal server error: /admin/profile/schoolprofile/add/ traceback (most recent call last):   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute return database.cursor.execute(self, query, params) sqlite3.integrityerror: not null constraint failed: perfil  escolar.create_date  above exception direct cause of following exception:  traceback (most recent call last):   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner response = get_response(request)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response     response = self.process_exception_by_middleware(e, request)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/contrib/admin/options.py", line 551, in wrapper return self.admin_site.admin_view(view)(*args, **kwargs)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/utils/decorators.py", line 149, in _wrapped_view response = view_func(request, *args, **kwargs)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func response = view_func(request, *args, **kwargs)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 224, in inner return view(request, *args, **kwargs)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1508, in add_view return self.changeform_view(request, none, form_url, extra_context)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/utils/decorators.py", line 67, in _wrapper return bound_func(*args, **kwargs)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/utils/decorators.py", line 149, in _wrapped_view response = view_func(request, *args, **kwargs)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/utils/decorators.py", line 63, in bound_func return func.__get__(self, type(self))(*args2, **kwargs2)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1408, in changeform_view return self._changeform_view(request, object_id, form_url, extra_context)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1448, in _changeform_view self.save_model(request, new_object, form, not add)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/contrib/admin/options.py", line 979, in save_model obj.save()   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/models/base.py", line 807, in save force_update=force_update, update_fields=update_fields)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/models/base.py", line 837, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/models/base.py", line 923, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/models/base.py", line 962, in _do_insert using=using, raw=raw)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/models/manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/models/query.py", line 1076, in _insert return query.get_compiler(using=using).execute_sql(return_id)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 1107, in execute_sql cursor.execute(sql, params)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/backends/utils.py", line 80, in execute return super(cursordebugwrapper, self).execute(sql, params)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise raise value.with_traceback(tb)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params)   file "/users/francisco.benavides/.pyenv/versions/django311/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute return database.cursor.execute(self, query, params) django.db.utils.integrityerror: not null constraint failed: perfil escolar.create_date 

it seems date time filed(create_date) not null ,i think not assign date filed value, there 2 handle error

if datetime field required,you can like

class schoolprofile(models.model):         create_date = models.datetimefield(                    null=false, blank=false,                    verbose_name=u'fecha de creación',auto_now_add=true) 

i have add key word in create_date filed when insert new record takes current date time local timezone

if datetime filed not required

class schoolprofile(models.model):         create_date = models.datetimefield(                    null=true, blank=true,                    verbose_name=u'fecha de creación') 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -