0

When I save form with empty date and date_added fields in django-admin site then it saves empty values there. I want to save current time instead. Here's code:

from django.db import models
import datetime
from django.utils import timezone

# Create your models here.

class Event(models.Model):
    def __str__(self):
        return self.title

    title = models.CharField(max_length=300)
    date = models.DateField(default=datetime.date.today, blank=True, null=True)
    date_added = models.DateTimeField(default=timezone.localtime, blank=True, null=True)
    text = models.TextField()

class EventPhoto(models.Model):
    event_id = models.ForeignKey(Event, on_delete=models.CASCADE)
    photo = models.ImageField(upload_to='news/')

Why default=timezone.localtime doesn't work?

Qback
  • 4,310
  • 3
  • 25
  • 38

3 Answers3

1

Django DateField provides auto_now_add and auto_now arguments for this:

date = models.DateField(auto_now=True, blank=True, null=True)
date_added = models.DateTimeField(auto_now_add=True, blank=True, null=True)
Arusekk
  • 827
  • 4
  • 22
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
  • 2
    AFAIK I won't be able to change date later if I'll use it - [best comment to answer here](https://stackoverflow.com/questions/2771676/django-datetime-issues-default-datetime-now) – Qback Feb 23 '18 at 09:20
  • @Qback yes, since you need default, I suppose you have to remove `null=True, blank=True` in your fields declaration. – neverwalkaloner Feb 23 '18 at 09:28
  • But when I don't use them I'm not able to save form with empty fields in `django-admin`. Also when I tried `CharField` with `blank=True` and `default="sometext"` it worked.. – Qback Feb 23 '18 at 09:30
  • @Qback if you leave only `blank=True` without `null=True` it should make empty field able. And for empty field `default` will be used. – neverwalkaloner Feb 23 '18 at 09:33
  • It throws **Exception** then. `IntegrityError` `NOT NULL constraint failed: news_event.date`. I guess it doesn't accept data I provide do `DateField`. I'm using `timezone.now` now, and still not working. – Qback Feb 23 '18 at 09:52
  • @Qback after that change you need to use makemigrations and migrate, after that it will work – Exprator Feb 23 '18 at 10:02
  • @Exprator yes, yes, I do it. – Qback Feb 23 '18 at 10:51
0

timezone.localtime is a conversion function, not a value, and that's why it cannot be used.

Help on function localtime in module django.utils.timezone:

localtime(value, timezone=None)
    Converts an aware datetime.datetime to local time.

    Local time is defined by the current time zone, unless another time zone
    is specified.
Arusekk
  • 827
  • 4
  • 22
0

I've figured out, that the problem is with django-admin. If I'll use code like this:

new_event = Event(title="blablah", text="some_text")
new_event.save()

then it saves DateField properly using default=timezone.localtime. Probably django-admin tried to pass empty string to DateField instead of not touching it and letting it take default action.

Qback
  • 4,310
  • 3
  • 25
  • 38