Opened 15 years ago

Closed 15 years ago

Last modified 12 years ago

#11612 closed (invalid)

Model field 'Default' argument doesn't work as documented

Reported by: andyortlieb Owned by:
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords: default, value, model, field
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

According to the docs (I am using 1.0.2 final) at http://docs.djangoproject.com/en/1.0/topics/db/models/

"default

The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created."

But the behavior I'm seeing looks like this:

>>> from application.models import *
>>> wi,il,ny = OptsState(State='Wisconsin',Abbr='WI'), OptsState(State='Illinois',Abbr='IL'),OptsState(State='New York',Abbr='NY')
>>> wi.uuid
u'02b01c71-f2ae-4d5f-82b1-88660521826a'
>>> il.uuid
u'02b01c71-f2ae-4d5f-82b1-88660521826a'
>>> ny.uuid
u'02b01c71-f2ae-4d5f-82b1-88660521826a'

The model looks like this:

class OptsState(models.Model):
    uuid = models.CharField(primary_key=True, editable=False, default=str(uuid.uuid4()))
    State = models.CharField(max_length=16) #Wisconsin, etc...
    Abbr = models.CharField(max_length=16) #wi, etc...
    def __unicode__(self):
        return self.State

Note, I'm using CharField for simplicity's sake. I have been using a subclass that returns 'uuid' from db_type for my database server. I've tried a number of other field classes as well but the result is the same.

It seems to me that it calls uuid.uuid4() ever time a new model is created, but not every time a new object is created.

Change History (2)

comment:1 by Alex Gaynor, 15 years ago

Resolution: invalid
Status: newclosed

Please don't use the ticket tracker for support questions. Use the django-users mailiing list or the #django freenode channel.

comment:2 by Anssi Kääriäinen, 12 years ago

Component: ORM aggregationDatabase layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top