Opened 8 years ago

Closed 8 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 Karen Tracey)

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')

Change History (3)

comment:1 Changed 8 years ago by Karen Tracey

Description: modified (diff)

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 8 years ago by wim@…

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


Request Method: POST
Request URL: http://localhost:8000/edit_event/
Django Version: 1.0-final-SVN-unknown
Python Version: 2.5.2
Installed Applications:
Installed Middleware:

File "/usr/lib/python2.5/site-packages/django/core/handlers/" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/wim/peoplecode/../peoplecode/" in wrapper
  11.         return func(request, *args, **kwargs)
File "/home/wim/peoplecode/../peoplecode/event/" in edit_event
  45.                 EventManager().set(event.cleaned_data, place_id, ownerid, event_id)
File "/home/wim/peoplecode/event/" in set
File "/usr/lib/python2.5/site-packages/django/db/models/" in save
  307.         self.save_base(force_insert=force_insert, force_update=force_update)
File "/usr/lib/python2.5/site-packages/django/db/models/" in save_base
  379.                 result = manager._insert(values, return_id=update_pk)
File "/usr/lib/python2.5/site-packages/django/db/models/" in _insert
  138.         return insert_query(self.model, values, **kwargs)
File "/usr/lib/python2.5/site-packages/django/db/models/" in insert_query
  888.     return query.execute_sql(return_id)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/" in execute_sql
  308.         cursor = super(InsertQuery, self).execute_sql(None)
File "/usr/lib/python2.5/site-packages/django/db/models/sql/" in execute_sql
  1700.         cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/django/db/backends/" 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 8 years ago by Karen Tracey

Resolution: invalid
Status: newclosed

You need null=True as well as blank=True so the database will allow null values for that column (see Since the table has been created with the wrong constraint, at this point you will need to either modify it manually or reset the app.

Note: See TracTickets for help on using tickets.
Back to Top