Django

Code

Ticket #1030 (closed: invalid)

Opened 3 years ago

Last modified 3 months ago

auto_now_add is broken for edit_inline field

Reported by: titoo Assigned to: nobody
Milestone: Component: django.contrib.admin
Version: SVN Keywords:
Cc: ibonso@gmail.com, brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com, matt.barry@dotwell.org, brooks.travis@gmail.com, smoonen@andstuff.org Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 1 Patch needs improvement: 1

Description

Considering the following models:

from django.core import meta

class Testcontact(meta.Model):
    name = meta.CharField(maxlength=100)
    birth_date = meta.DateField(blank=True, null=True)
    time_added = meta.DateTimeField(auto_now_add=True)
    time_modified = meta.DateTimeField(auto_now=True)

    def __repr__(self):
        return self.name

    class META:
        admin = meta.Admin()

class Testaddress(meta.Model):
    email = meta.EmailField(core=True)
    time_added = meta.DateTimeField(auto_now_add=True)
    time_modified = meta.DateTimeField(auto_now=True)
    contact = meta.ForeignKey(Testcontact, edit_inline=meta.STACKED, min_num_in_admin=2)

    def __repr__(self):
        return self.email

When adding a "contact" as long as no emails are provided, everything is fine. Then if you add an email, the first save is ok, the second one will return an error:

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/testcase/testcontacts/1/
Exception Type: OperationalError?
Exception Value: testcase_testaddresss.time_added may not be NULL
Exception Location: /usr/lib/python2.4/site-packages/django/core/db/backends/sqlite3.py in execute, line 71

As shown the backend is sqlite, django revision 1568.

Attachments

django_1030.patch (0.8 kB) - added by yk4ever@gmail.com on 07/27/06 17:07:49.
Temporary fix

Change History

(follow-up: ↓ 25 ) 12/09/05 06:54:14 changed by titoo

Replacing the auto_now_add with:

time_added = meta.DateTimeField(default=meta.LazyDate(), blank=True, editable=False)

Solves the problem.

02/05/06 05:12:18 changed by anonymous

  • version set to SVN.

I can reproduce this error with this model and django revision 2279, also sqlite backend :

class File(meta.Model):
    """This class manages a File object
       This basically add some metadata to a FielField"""

    title = meta.CharField(maxlength=50)
    slug = meta.SlugField(prepopulate_from=('title',))
    description = meta.TextField(maxlength=255)
    # creation_date should receive auto_now_add, but it seems to interact
    # weirdly with FileField
    creation_date = meta.DateTimeField(auto_now_add=True)
    modification_date = meta.DateTimeField(auto_now=True)
    file = meta.FileField(upload_to='files/%Y/%m/')

The error is nearly the same. Only the field's name is (obviously) different. Note that there's no error if I remomve the "_add" from the "creation_date" field OR if I comment the "file" field out (and reset the database)

02/05/06 05:18:34 changed by anonymous

  • cc set to brice.carpentier@daknet.org.

02/08/06 22:33:37 changed by Brian Ray <bray@sent.com>

  • cc changed from brice.carpentier@daknet.org to brice.carpentier@daknet.org, bray@sent.com.

I see this also.

Here is my error query:

ERROR: null value in column "createdate" violates not-null constraint UPDATE "abc_inventoryitems" SET "groupcode"='SEA',"language"='',"description"='Core LHD',"itemcode"='9201.35',"printteam"=22200,"reportid_id"='1',"createdate"=NULL WHERE "id"='1'

and my model:

class InventoryItem(meta.Model):   
   groupcode = meta.CharField("Section Code",maxlength=10,help_text="Code for Section, ie ABX, SEA,...")   
   language = meta.CharField("Language", choices=LANGUAGES, null=True, maxlength=50, blank=True)   
   description = meta.CharField("Item Description",  maxlength=30, help_text="Short desciption, i.")   
   itemcode = meta.CharField("Unique Item Code", maxlength=30, help_text="Unique Vendor supplied code.")   
   printteam = meta.IntegerField("Printteam Code:", maxlength=6, help_text="Five digit printstream code")               
   reportid = meta.ForeignKey(InventoryReport, verbose_name="Inventory Item", edit_inline=meta.TABULAR,num_in_admin=10)   
   createdate = meta.DateTimeField("Created Date", auto_now_add=True)

Although the first time a added a bank of tabular entries it worked file. However, I get this error only when I add the 11th (after the first bank of ten was full). If I come back and try to add one, I still get this error.

-- Brian Ray <bray@sent.com>

04/25/06 14:39:40 changed by jan

  • cc changed from brice.carpentier@daknet.org, bray@sent.com to brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de.
  • priority changed from normal to high.

Bug persists: Only occurs when adding a referenced object to an existing entry, i.e. adding a new mailbox to a domain. adding separately works, inline edit fails with database exception due to null-field

05/25/06 20:44:22 changed by Ido

  • cc changed from brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de to brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl.
  • component changed from Admin interface to Database wrapper.
  • severity changed from normal to major.

Bug persists into magic-removal branche too, i get a sqlerror with this code:

new_child = manipulator.save(new_data) parent.child_set.add(new_child)

where none on the auto_add* model.DateTimeFields? are set in new_data figuring that auto_add and auto_add_now (for creation and modification date) will take care or those fields.

now the error: (only the header, using the sqlite backend) Request Method: POST Request URL: http://localhost/edit/parent/2/addChild/ Exception Type: OperationalError? Exception Value: children_child.cre_date may not be NULL Exception Location: /usr/src/Django-trunk/django/db/backends/sqlite3/base.py in execute, line 73

changed properties: component: admin -> database wrapper, cause my errors where in views.py not the admin pages severity: normal -> major, cause i found that in many cases this defect renders auto_add(_now) unusable (out of the box) for creation and modification fields.

05/31/06 11:06:54 changed by asmodai@in-nomine.org

Could you people try the current SVN trunk?

Luke Plant fixed something for auto_now_add which fixes the UPDATE after the initial INSERT TO to have the right datestamp and not a NULL value.

05/31/06 12:31:44 changed by Ido

i've quickly tested the code in svn trunk and it looks okey. i'll be coding more on the model with lots of auto_now and auto_now_add functions near the end of this week, if i'll find errors i'll post a comment here.

07/04/06 04:23:39 changed by Pete Crosier

  • cc changed from brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl to brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com.

Still reproduceable in current SVN, with the same experience as Brian above.

I'm showing 1 editable inline object (it seems Brian was showing 10), the first of which is created without a hitch. When I go back to create object number 2 (or in Brian's case, 11), it fails with a "null value in column "my_date_field" violates not-null constraint UPDATE.." (using PostgreSQL).

07/27/06 17:07:49 changed by yk4ever@gmail.com

  • attachment django_1030.patch added.

Temporary fix

08/01/06 06:06:29 changed by yk4ever@gmail.com

Problem located: django_src/django/db/models/fields/init.py DateField?.get_follow()

This method returns True for auto_now_add fields - therefore, they are counted as enabled in admin section, and their value is overwritten by None.

Removing this method solves the bug, but I don't sure about side effects.

10/12/06 05:57:48 changed by bjorn@exoweb.net

  • cc changed from brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com to brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net.

Adding myself and a colleague to the cc for this ticket. We're having this problem.

11/06/06 10:21:54 changed by Gary Wilson <gary.wilson@gmail.com>

  • cc changed from brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net to brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com.

11/07/06 02:47:33 changed by Nolik

11/08/06 02:33:22 changed by prom dresses

11/28/06 08:36:10 changed by Jyrki Pulliainen <jyrki.pulliainen@gmail.com>

  • cc changed from brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com to brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com.

Easy bypass, without changing django source, for this is a custom save method:

def save(self):
    if self.id:
        this_object = Object.objects.filter(id__exact=self.id)[0]
        self.add_date = this_object.add_date
    super(Object, self).save()

This, however, overwrites the changes if the field is editable (probably not, if auto_add_now is used?)

02/04/07 05:23:49 changed by anonymous

  • cc changed from brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com to ibonso@gmail.com.

02/07/07 12:45:33 changed by anonymous

  • cc changed from ibonso@gmail.com to ibonso@gmail.com, brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com.

02/14/07 11:54:21 changed by anonymous

  • cc changed from ibonso@gmail.com, brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com to ibonso@gmail.com, brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com, matt.barry@dotwell.org.

02/17/07 22:22:01 changed by Gary Wilson <gary.wilson@gmail.com>

  • needs_better_patch set to 1.
  • component changed from Database wrapper to Admin interface.
  • needs_tests set to 1.
  • summary changed from auto_now_add is broken for inline edited field to auto_now_add is broken for edit_inline field.
  • has_patch set to 1.
  • stage changed from Unreviewed to Accepted.

Changed summary to include "edit_inline". The patch is described as a temp fix.

02/21/07 02:26:07 changed by brooks.travis@gmail.com

  • cc changed from ibonso@gmail.com, brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com, matt.barry@dotwell.org to ibonso@gmail.com, brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com, matt.barry@dotwell.org, brooks.travis@gmail.com.

I'm having a similar issue in a view I've recently created using newforms. When I try to modify an existing model object, I get the following error: "year is out of range". I'm using MySQL 5.0.22. I've gone into the DB tables themselves and checked the data. After the initial object creation, the create date is set properly. However, after any subsequent save/update of the object, the create date is set back to all zeroes. This DOES NOT occur in the admin interface, only in my custom view using the newforms save() method.

(follow-up: ↓ 22 ) 05/22/07 17:30:33 changed by SmileyChris

  • status changed from new to closed.
  • resolution set to fixed.

auto_now and auto_now_add have been removed altogether

(in reply to: ↑ 21 ; follow-up: ↓ 23 ) 06/04/07 04:03:30 changed by ubernostrum

  • status changed from closed to reopened.
  • resolution deleted.

Replying to SmileyChris:

auto_now and auto_now_add have been removed altogether

Nope. Still kicking around, much to my chagrin. Check django/db/models/__init__.py to verify that they're still in the codebase :(

(in reply to: ↑ 22 ) 06/04/07 15:47:49 changed by SmileyChris

Replying to ubernostrum:

Nope. Still kicking around, much to my chagrin. Check django/db/models/__init__.py to verify that they're still in the codebase :(

So shouldn't that be opened as a new ticket (perhaps referencing this one)? I'm sure the goal was to eliminate those.

07/11/07 11:48:33 changed by anonymous

Looked in django/db/models/fields/init.py and saw that DateField? has the following get_follow() method:

    # Needed because of horrible auto_now[_add] behaviour wrt. editable
    def get_follow(self, override=None):
        if override != None:
            return override
        else:
            return self.editable or self.auto_now or self.auto_now_add

BUT, DateTimeField? has the following get_follow() instead:

    def get_follow(self, override=None): 
        if override != None:
            return override
        else:
            return self.editable

Shouldn't they be the same? I have DateTimeFields? with auto_add_now and they are causing the same problem described.

(in reply to: ↑ 1 ; follow-up: ↓ 26 ) 07/11/07 15:57:08 changed by drbob@dokterbob.net

Replying to titoo:

Replacing the auto_now_add with: {{{ time_added = meta.DateTimeField?(default=meta.LazyDate?(), blank=True, editable=False) }}}

LazyDate? does not seem to be present at all in the current SVN nor does the posted patch work. Any suggestions?

(in reply to: ↑ 25 ; follow-up: ↓ 29 ) 07/11/07 19:46:00 changed by gwilson

Replying to drbob@dokterbob.net:

LazyDate? does not seem to be present at all in the current SVN nor does the posted patch work. Any suggestions?

LazyDate was removed a little while back. But now the default parameter takes callables, so you can do this:

from datetime import datetime
...
time_added = meta.DateTimeField(default=datetime.now(), blank=True, editable=False)

05/08/08 19:42:12 changed by Scott Moonen <smoonen@andstuff.org>

  • cc changed from ibonso@gmail.com, brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com, matt.barry@dotwell.org, brooks.travis@gmail.com to ibonso@gmail.com, brice.carpentier@daknet.org, bray@sent.com, djangodev@23t.de, root.lastnode@hccnet.nl, pete.crosier@gmail.com, bjorn@exoweb.net, chengqi@exoweb.net, gary.wilson@gmail.com, jyrki.pulliainen@gmail.com, matt.barry@dotwell.org, brooks.travis@gmail.com, smoonen@andstuff.org.

I'm seeing this problem as well. I tracked it down to DateField.get_follow before finding this defect. Removing DateField.get_follow and allowing things to fallback to the Field.get_follow behavior fixes things for me.

07/13/08 05:31:24 changed by anonymous

(in reply to: ↑ 26 ) 08/01/08 16:54:34 changed by lukeplant

Replying to gwilson:

That should be:

from datetime import datetime
...
time_added = meta.DateTimeField(default=datetime.now, blank=True, editable=False)

09/05/08 19:29:17 changed by brosner

  • status changed from reopened to closed.
  • resolution set to invalid.

edit_inline has been completely refactored from newforms-admin in 1.0. This likely works, aside from the state of auto_now_add. Marking invalid.


Add/Change #1030 (auto_now_add is broken for edit_inline field)




Change Properties
Action