Code

Opened 2 years ago

Closed 2 years ago

Last modified 2 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.

Attachments (0)

Change History (7)

comment:1 Changed 2 years ago by cmsimike@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Can't assign values for models.DateTimeField(blank=True, null=True) to 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 Changed 2 years ago by cmsimike@…

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

comment:3 Changed 2 years ago by apollo13

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 Changed 2 years ago by anonymous

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 Changed 2 years ago by carljm

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

comment:6 Changed 2 years ago by carljm

  • Resolution set to needsinfo
  • Status changed from new to closed

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

comment:7 Changed 2 years ago by anonymous

ah crap, im sorry.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.