teewuane
teewuane

Reputation: 5734

How to nullify a django datetime field that has an existing value?

I have a model that has an optional datetime field.

class Order(models.Model):
    ...
    fulfilled = models.DateTimeField(blank=True, null=True)
    ...

I can update the record's datetime to an actual datetime...

order = Order.objects.get(pk=99)
order.fulfilled = datetime.datetime.now()
order.save()

or... 

order.update(fulfilled=datetime.datetime.now())

Occasionally I need to remove the field's value or set it to an empty value. I can not figure out how to do this...

order = Order.objects.get(pk=99)
order.fulfilled = null #? or '' or... 0 or... 
order.save()

Upvotes: 1

Views: 1902

Answers (1)

teewuane
teewuane

Reputation: 5734

I just figured this out.

I need to set fulfilled to None.

order = Order.objects.get(pk=99)
order.fulfilled = None
order.save()

Upvotes: 3

Related Questions