Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#17836 closed Bug (needsinfo)

Can't assign values for models.DateTimeField(blank=True, null=True)

Reported by: cmsimike@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4-beta-1
Severity: Release blocker Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using Django 1.4b1 with timezone aware datetime objects

It seems to be the case that I cannot assign a value to a model's field and persist it to the database. The field is defined as
asked_on = models.DateTimeField(blank=True, null=True)
and I've tried datetime.now and timezone.now()

What happens is this:
model.asked_on = datetime.now() (or timezone.now())
print model.asked_on # None is printed to the console
model.save() #sql logs show a null value being put into the update query.

Change History (7)

comment:1 by cmsimike@…, 12 years ago

Summary: Can't assign values for models.DateTimeField(blank=True, null=True)i should have previewed how the code would look

Sorry, I should have previewed i guess.

model.asked_on = datetime.now() (or timezone.now())
print model.asked_on # None is printed to the console
model.save() #sql logs show a null value being put into the update query.

comment:2 by cmsimike@…, 12 years ago

Summary: i should have previewed how the code would lookCan't assign values for models.DateTimeField(blank=True, null=True)

comment:3 by Florian Apolloner, 12 years ago

I can't reproduce this on Django 1.4 RC 1 using sqlite and pg. Please tell us which database you use and provide some code we can use to reproduce.

comment:4 by anonymous, 12 years ago

Using MySQL. I know that mysql doesn't support timezone aware datetimes but does it make sense for the model to not accept values?

class MyModel(models.Model):
    #various other fields
    answered_on = models.DateTimeField(blank=True, null=True)
 
modelToChange = MyModel.objects.get(pk=PK)
form = MyModelForm(request.POST)
 if form.is_valid():
     value = form.cleaned_data['value']
     modelToChange.answer = answer
     modelToChange.answrerd_on = timezone.now() # or using datetime.now(), same problem
     print modelToChange.answered_on # this prints None regardless of previous method 
     modelToChange.save() # generates a answered_on = NULL in the sql

comment:5 by Carl Meyer, 12 years ago

Is the answrerd_on typo copied from your actual code? Because that would fully explain the behavior you are seeing.

comment:6 by Carl Meyer, 12 years ago

Resolution: needsinfo
Status: newclosed

Closing as needsinfo pending reproduction code sans typo that still displays the problem.

comment:7 by anonymous, 12 years ago

ah crap, im sorry.

Note: See TracTickets for help on using tickets.
Back to Top