Code

Opened 5 years ago

Closed 5 years ago

Last modified 14 months 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: UI/UX:

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.

Attachments (0)

Change History (2)

comment:1 Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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

comment:2 Changed 14 months ago by akaariai

  • Component changed from ORM aggregation to Database layer (models, ORM)

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.