nsx
nsx

Reputation: 705

DateTimeField set as default field

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?

Upvotes: 3

Views: 4694

Answers (1)

user764357
user764357

Reputation:

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.

Upvotes: 2

Related Questions