On Wed, 2006-08-02 at 18:41 -0700, bernie2004 wrote: > i would really like to know if there is a way around getting > the same values when adding new entries through the admin > when using defaults like those: > > models.CharField( default=random.randint(1,1000) ) > or > models.CharField( default=time.time() ) > > or in other words: > is there any way to insert by default a unique value into a field from > the model-side?
The reason you are getting the same values all the time is because the random.randint(1,1000) and time.time() calls are being evaluated when the CharFiueld instance is created, which is at your model *class* creation time (i.e. import time, essentially). Not when you create a model instance. What you want is to pass in a callable for the default value (not the result of calling the callable, which is what you are doing now). See http://www.djangoproject.com/documentation/models/field_defaults/ for an example. Another way to do this would be to create the value as part of your save() method, if it was dependent on other things. But for a simple thing like time.time, just using a callable default will do. Realise that for your random.randint case, you will need to create a function that returns a new value in the range when it is called. You could use lambda: random.randint(1, 1000) and it should work. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---