Fivapr Fivapr -4 years ago 190
Python Question

How to set empty value to DateField in django model?

What am I doing wrong here? I need to set empty value to doing_field.

def delete_todo_today(request):
if request.is_ajax() and request.POST and request.user.is_authenticated:
thingtodo = ThingToDo.objects.filter(author = request.user, thing = request.POST.get('item'))
thingtodo.doing_date = None
data = {'message': "%s 's doing_date deleted" % request.POST.get('item')}
return HttpResponse(json.dumps(data), content_type='application/json')
raise Http404

Answer Source

You are using .filter() to get the objects for thingtodo. This will return a QuerySet or simply said a list of objects. You can not set an attribute on multiple objects directly.

If you wat to retrieve one object only, use get() instead of .filter(). This will raise an exception when there it more or less than one object matching.

If you want to update multiple objects, use the update() method of the QuerySet:


Also make use that you model allows Null for the attribute.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download