These are chat archives for django/django

16th
Jun 2018
Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 02:50
hey can anyone explain how the USE_TZ and TIME_ZONE settings work?
if i do timezone.now(), it returns a UTC timestamp that's also timezone aware
if i save that value to the DB
it looks like as part of the saving process, it localizes the time to what I have set in TIME_ZONE?
is that what's supposed to happen?
Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 02:56
just found that as you pasted it :) thank you!
looks like it is expected that it coerce your dates to what's in TIME_ZONE, if USE_TZ is true
at least, if I'm reading it right.
Brett MacDonald
@ArtistsTechGuy
Jun 16 2018 02:59
yes it also creates them as timzone aware objects rather then the old style (which it tries to convert anyways) if I'm reading it right
Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 02:59
what i'm curious is
if USE_TZ = False
then all your datetimes are naive
in which case, I would really wonder how it would be able to coerce anything to what's in your TIME_ZONE
maybe it doesn't?
or maybe it just assumes it is what is in TIME_ZONE
aka all naive time zones are now going to be tagged with TIME_ZONE's value
idk, that paragraph isn't super clear to me
Brett MacDonald
@ArtistsTechGuy
Jun 16 2018 03:06
if USE_TZ is true it uses
from django.utils import timezone

now = timezone.now()

else

import datetime

now = datetime.datetime.now()

if when it gets to the db layer it will try to convert to TIME_ZONE but it may make mistakes concerning DST

Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 03:36
how can it convert datetime.datetime.now() to TIME_ZONE if it's a naive datetime?
it's like being given plainly "4:30pm" and told to convert it to Eastern US Time
Brett MacDonald
@ArtistsTechGuy
Jun 16 2018 03:38
it probably stores it as a django timezone object
Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 03:38
yeah, like it just assumes "4:30pm" is already in Eastern US Time
and then stores it as such
(i'm guessing here)
Brett MacDonald
@ArtistsTechGuy
Jun 16 2018 03:38
yes I think so
best way would be to just write up a bit of code like in the example further down the page and use both types
to test it
most of the django objects already use the timezone .now() way so unless you are specificly using datetime objects you shouldn't have any trouble either way
Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 03:42
at work, we've got our TIME_ZONE = 'America/New_York'
some of our tests fail based on the time of day
Brett MacDonald
@ArtistsTechGuy
Jun 16 2018 03:42
ahhh gotcha
Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 03:42
like if you are coding in New York late into the evening but UTC has a datetime in the "future"
it seems like we should have done TIME_ZONE = 'UTC' always
with USE_TZ = True
and then when we need to use dates we use something like pytz to get it into EST
err -- when we need to display dates
prolly best to keep dates as UTC for as long as possible until a human in NYC has to see it
Brett MacDonald
@ArtistsTechGuy
Jun 16 2018 03:48
yea, I'm unsure if it is stored as 04:00UTC -5:00EST in the db or just 23:00, if it is the former then you can probably find a way to fix it using https://docs.djangoproject.com/en/2.0/ref/utils/#time-zone-selection-functions
just create a new object and check the stored value if it is in UTC+modifier or naive and asumes TZ then convert acordingly
Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 03:51
yeah
for our setup it looks like the latter
it's stored in our postgres db naive
so we have to do some conversions to fix the tests
i wonder if there's a setting for postgres backends to store with timezone info
Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 03:55
ah
yeah if we do a read from the DB
we can use make_aware to basically set the tz_info on the datetime object
so that we know that the datetime is of the datetime in TIME_ZONE
Brett MacDonald
@ArtistsTechGuy
Jun 16 2018 03:57
when they are tz_aware they are always set as utc times and you can convert them with localtime()
Christopher Sabater Cordero
@cs-cordero
Jun 16 2018 03:58
hm?
make_aware just localizes any naive timestamp to what is returned by get_current_timezone()
and your current_timezone is what is set in TIME_ZONE, or what you selected with activate(), if you used it previously
so assuming you didn't use activate() yet, they make_aware doesn't always set as utc times
garynobles
@garynobles
Jun 16 2018 10:57
I can add the current user to the database though the created by is set and the times, when I edit I would like the modified by to be updated, how do I implement that? Optionally I'd like the modified date/modified_by only when the recorded has been modified, not included when it is created, although this last request is optional.
Heres the views.py
def createstore(request):
    if request.method == "POST":
        form = StorageForm(request.POST)
        if form.is_valid():
            post = form.save(commit=False)
            post.created_by = request.user
            post.modified_by = request.user
            post.datetime = datetime.datetime.now()
            post.save()
            return redirect('allstorage')
    else:
        form = StorageForm()
    return render(request, 'store/create_store.html', {'form': form})
:]
@elcolie
Jun 16 2018 14:43

@garynobles

class AbstractTimestamp(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

Normally Django people do this

garynobles
@garynobles
Jun 16 2018 14:46
ah, is my code above doing anything? Since I have this already in my models.py file?
    created_by = models.CharField(max_length=200)
    created_timestamp = models.DateTimeField(auto_now_add=True)
    modified_by = models.CharField(max_length=200,editable=True)
    modified_timestamp = models.DateTimeField(auto_now=True)
should I remove the datetime line?
:]
@elcolie
Jun 16 2018 14:47
@garynobles Delete you created_timestamp and modified_timestamp then use the given Mixin
created_by, modified_by. They are ForeignKey. Any reason to use CharField?
garynobles
@garynobles
Jun 16 2018 14:50
ok, thanks. Modified by is a username, hence charfield, I didn't go down the foreign key route since this is logging who has entered data, if we delete a user we want to keep the record of who entered the data
Also do you @elcolie (or anyone else) know why I would be getting an error from django.forms.widgets import flatatt
ImportError: cannot import name 'flatatt' - this is when I put it on a windows 2012 R2 server
:]
@elcolie
Jun 16 2018 14:52
@garynobles By that you can set on_delete=models.SET_NULL or whatever
https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.ForeignKey
For the ImportError is very obvious that you need to install the package. Just use pip install
garynobles
@garynobles
Jun 16 2018 14:54
i have widget_tweaks installed, i have to install flatatt in addition?
it wasn't needed on my local machine, but i'llgive it a go
:]
@elcolie
Jun 16 2018 14:56
@garynobles wagtail/wagtail@7d8a1a0
Try this
garynobles
@garynobles
Jun 16 2018 14:59
just one second...
pip3 install flatatt - could not find version that satisfies the requirement flatatt
is it called something else like django_flatatt?
same error for django_flatatt
:]
@elcolie
Jun 16 2018 15:01
@garynobles Have you tried my given comit above?
widgets -> utils
garynobles
@garynobles
Jun 16 2018 15:03
sorry bear with me, I feel like a noob (trained in HTML 4!)
I'm also fairly new to github, so you might have to spell it out to me, my apologies
I'm guessing I need to find the wagtails directory and make this change manually?
:]
@elcolie
Jun 16 2018 15:05
Possibly
but why don't you change a new version. Since it supposed to be a bug by now
What's the point of reviving a dinosaur?
garynobles
@garynobles
Jun 16 2018 15:07
I'm an archaeologist, not a paleontologist
you mean django 2.0.6?
:]
@elcolie
Jun 16 2018 15:08
I am not using wagtail. I don't know what version it supports
garynobles
@garynobles
Jun 16 2018 15:09
you mean pip install wagtail rather than widget_tweaks?
oh wagtail is a cms
:]
@elcolie
Jun 16 2018 15:13
@garynobles No idea on this issue. Sorry.
garynobles
@garynobles
Jun 16 2018 15:14
ok, thanks for trying to help... I'm only one week 2 of using Django
:]
@elcolie
Jun 16 2018 15:15
If you are just start with that. Try naked Django first. Do not add too much furniture. Expecially Wagtial!
garynobles
@garynobles
Jun 16 2018 15:15
so I need to strip out all the widget_tweaks in my html to get over this error?
:]
@elcolie
Jun 16 2018 15:19
@garynobles Let's move to private chat