Skip to content

Instantly share code, notes, and snippets.

@grizmio
Last active September 5, 2024 23:04
Show Gist options
  • Save grizmio/14418861b5782880304787ba22861c19 to your computer and use it in GitHub Desktop.
Save grizmio/14418861b5782880304787ba22861c19 to your computer and use it in GitHub Desktop.
# https://stackoverflow.com/questions/7499767/temporarily-disable-auto-now-auto-now-add
"""
Con modifcacion de Georgina S
n.b. (1.10) ModelClass._meta.get_field_by_name(field_name)[0] in do_to_model didn't seem to be working for me - changed to: ModelClass._meta.get_field(field_name) –
Georgina S
Commented Mar 8, 2017 at 1:15
"""
def turn_off_auto_now(ModelClass, field_name):
def auto_now_off(field):
field.auto_now = False
do_to_model(ModelClass, field_name, auto_now_off)
def turn_off_auto_now_add(ModelClass, field_name):
def auto_now_add_off(field):
field.auto_now_add = False
do_to_model(ModelClass, field_name, auto_now_add_off)
def do_to_model(ModelClass, field_name, func):
field = ModelClass._meta.get_field(field_name)
func(field)
turn_off_auto_now(Foo, 'fkng_field')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment