Fivapr Fivapr - 3 months ago 8
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
thingtodo.save()
data = {'message': "%s 's doing_date deleted" % request.POST.get('item')}
return HttpResponse(json.dumps(data), content_type='application/json')
else:
raise Http404

Answer

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:

thingtodo.update(doing_date=None)

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

Comments