DateTimeField set as default field

Question:

The following model needs a default value for its Date Field:

from django.utils import timezone as django_tz 
import psycopg2

class AvailabilityRuleOnce(AvailabilityRule):
    class Meta:
        app_label = 'configuration'

    objects = AvailabilityRuleOnceManager()

    starting_time = django_models.DateTimeField(
        'Starting time for the rule', default=django_tz.now, blank=True
    )
    ending_time = django_models.DateTimeField(
        'Ending time for the rule', default=django_tz.now, blank=True
    )

When trying to apply the migrations, the following exception is thrown:

django.db.utils.ProgrammingError: column "ending_time" cannot be cast automatically to type timestamp with time zone
HINT:  You might need to specify "USING ending_time::timestamp with time zone".

In the Django documentation they recommend this option and I have also tried other options that can be found on-line like adding "auto_now_add=True" but it is not working either. What am I doing wrong?

Asked By: nsx

||

Answers:

auto_now_true is the only way to set the field to update on creation, as the documentation states

The options auto_now_add, auto_now, and default are mutually exclusive. Any combination of these options will result in an error.

If auto_now_true isn’t working the you need to raise a bug.

Answered By: user764357
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.