Code

Opened 5 years ago

Closed 5 years ago

#9558 closed (invalid)

datetime blank=True

Reported by: wim@… Owned by: nobody
Component: Uncategorized Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by kmtracey)

In a model, having a DateTimeField with blank = True, I get an error when leaving the value blank on saving.

My model is, simplified:

class Place(models.Model):
    name = models.CharField(max_length=100)

class Event(models.Model):
    user = models.ForeignKey(User)
    place = models.ForeignKey(Place, blank=True)
    starttime = models.DateTimeField(blank=True)

class EventForm(forms.ModelForm):
    class Meta:
        model = Event
        exclude = ['owner']

def edit_event(request, event_id=False):
    userid = request.session['loggedinas']
    if request.POST:
        event = EventForm(request.POST)
        if event.is_valid():
            cleaned_data = event.cleaned_data
            event = Event( user_id = userid )
            fields = ['name', 'description', 'starttime', 'endtime', 'photo', 'file']
            for fieldname in fields:
                setattr( event, fieldname, cleaned_data[fieldname])
                return HttpResponseRedirect('/success')
     ...

Attachments (0)

Change History (3)

comment:1 Changed 5 years ago by kmtracey

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Reformatted description. Please use preview before posting. Also, it would be ever so much more helpful if you'd specify what exact error you get. Why leave us guessing with the vague "an error"?

comment:2 Changed 5 years ago by wim@…

The error is:
Exception Value: null value in column "starttime" violates not-null constraint

Environment:

Request Method: POST
Request URL: http://localhost:8000/edit_event/
Django Version: 1.0-final-SVN-unknown
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'peoplecode.adresbeheer',
 'peoplecode.almanac',
 'peoplecode.event',
 'peoplecode.fields',
 'peoplecode.friends',
 'peoplecode.general',
 'peoplecode.people',
 'peoplecode.selectionfield']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware')


Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/wim/peoplecode/../peoplecode/decorators.py" in wrapper
  11.         return func(request, *args, **kwargs)
File "/home/wim/peoplecode/../peoplecode/event/views.py" in edit_event
  45.                 EventManager().set(event.cleaned_data, place_id, ownerid, event_id)
File "/home/wim/peoplecode/event/models.py" in set
  71.         event.save()
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in save
  307.         self.save_base(force_insert=force_insert, force_update=force_update)
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in save_base
  379.                 result = manager._insert(values, return_id=update_pk)
File "/usr/lib/python2.5/site-packages/django/db/models/manager.py" in _insert
  138.         return insert_query(self.model, values, **kwargs)
File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in insert_query
  888.     return query.execute_sql(return_id)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/subqueries.py" in execute_sql
  308.         cursor = super(InsertQuery, self).execute_sql(None)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/query.py" in execute_sql
  1700.         cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/util.py" in execute
  19.             return self.cursor.execute(sql, params)

Exception Type: IntegrityError at /edit_event/
Exception Value: null value in column "starttime" violates not-null constraint

comment:3 Changed 5 years ago by kmtracey

  • Resolution set to invalid
  • Status changed from new to closed

You need null=True as well as blank=True so the database will allow null values for that column (see http://docs.djangoproject.com/en/dev/ref/models/fields/#null). Since the table has been created with the wrong constraint, at this point you will need to either modify it manually or manage.py reset the app.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.