How do I access an objects fields when it's being created from a form
MarkusH
Can you file a ticket, please. The problem is the way Django tries to detect what changed. And since all arguments passed into a Field are taken into account, Django compares now() with now(), which is obviously not the save
MarkusH: How could I modify my current model to avoid this ?
livingstn joined the channel
AntelopeSalad
is django-activelink one of the better packages for active/highlight page functionality?
moneydouble joined the channel
johnraz is now known as johnraz|away
MarkusH
TaiSHi: drop the default from the field, and override the save method: def save(*args, **kwargs): if not self.last_sync: self.last_sync = now(); super(...)
that's what I'd do
stahl has quit
schinckel
TaiSHi: I've commented on your ticket: you don't want to use default=datetime.now(), as that gets evaluated at import time.
TaiSHi: If you use default=datetime.now, it will actually work as you want it to (and probably won't keep thinking the column needs to be changed).
TaiSHi
Let me try
hyperair has quit
MarkusH: regarding your answer, I'm rather new to all of this, so your suggestion is basically no default and then define the default on save method ?
darylantony has quit
codeinthehole has quit
schinckel
TaiSHi: Indeed, last_sync having a default is worthless, as you'll want to update the value whenever a sync occurs.
MarkusH
TaiSHi: give schinckel's idea a try
TaiSHi
schinckel: It worked
darylantony joined the channel
AntelopeSalad
is that not a place to use add_now?
schinckel
AntelopeSalad: That's frowned upon in certain circumstances :)
stahl joined the channel
MarkusH
AntelopeSalad: might be
schinckel
But I probably would, although last sync implies something other than just last modified
TaiSHi
schinckel: yes, it should be updated everytime a sync occurs but what would be the initial value?
iqualfragile_ joined the channel
MarkusH
schinckel: nice shot. wasn't aware that default takes a function and evaluates it
schinckel
If no sync has occurred, then it should be None.
iqualfragile has quit
It depends what a 'sync' means.
TaiSHi
sync / update, it will query a wallet for current balance
schinckel
It may be that now is appropriate, but now() isn't, for the reason I outlined above.
moneydouble has quit
hwrdprkns joined the channel
moneydouble joined the channel
now() is just the same as typing the date at the time the module is first imported: it's a static value for that run of the program.
fllr has quit
TaiSHi
Understood
bite has quit
It's not quite a bug but it works as intended
bradfordtoney has quit
schinckel
No, it is a bug.
If you start your system now, and it runs for three days, it will keep using the time you started the system as the defaults for that field for new instances.
acidfu has quit
AntelopeSalad
hmm, so when you said passing it a callable, if you pass it the function without executing it, then it executes it on each instance creation?
MarkusH
schinckel: but then I'd use auto_now=True instead
which is more explicit in my eyes
breadfruitman has quit
windyhouser has quit
therick has quit
schinckel: the question is: shall Django warn a user about that?
vishen joined the channel
TaiSHi
Well, haven't even started to code my app and already found a bug
AntelopeSalad
i'm using auto_now with 1.6.1 with no warnings
MarkusH
using a "hard-coded" date/time/datetime object in the field.
TaiSHi
This might be foretelling
schinckel
Apparently there was a push to have auto_now and auto_now_add deprecated. I've never had problems with them, but having a default=datetime.datetime.now is probably more explicit and 'standard python' than auto_now_add.
As for auto_now, some people suggest overriding your save() method and putting it in there. Or there are a couple of mixins you can use to handle this.
IIRC, __love__'s django-braces has them, or was it django-model-utils?
mattmcc
auto_now* predates when default could take a callable, which is much cleaner.
schinckel
Probably the latter, actually.
m8 has quit
django-braces is CBV, django-model-utils is model/manager/field
mattmcc
Yeah, django-model-utils has created/modified fields, as well as the handy TimeStampedModel abstract model class.
AntelopeSalad
schinckel it's django-extensions
unless both happen to have it
tbaxter has quit
MarkusH
schinckel: isn't the difference, that auto_now* uses the database's NOW()?
TaiSHi
Having generated a decent chat
MarkusH
TaiSHi: hehe, yes
AntelopeSalad
from django_extensions.db.models import TimeStampedModel