Re: Pre-populating an integer field with a count of model objects
On Aug 21, 11:29 am, Daniel Roseman <[EMAIL PROTECTED]> wrote: > On Aug 21, 4:42 pm, Brandon Taylor <[EMAIL PROTECTED]> wrote: > > > Hi everyone, > > > I have a column, 'position', which is a PositiveIntegerField, to allow > > my end-user to order records with. I would like to pre-populate the > > field when creating a new record, with the count of the model objects > > + 1. > > > The models documentation says 'default' can be a value or a callable, > > but using self.objects.count() + 1 doesn't work, because 'self' hasn't > > been defined. > > > Can anyone point me to an example of how I can accomplish this? > > > TIA, > > Brandon > > 'self.objects.count() + 1' isn't a callable, it's an expression, which > you can't use here. (A callable is a function object, or a class with > __call__ defined.) > However, unfortunately even a callable probably wouldn't help you > here, as the implementation calls it without any parameters. It's > really just for things like inserting the current time, which doesn't > need parameters. > Seehttp://www.djangoproject.com/documentation/models/field_defaults/ > > You can achieve what you want by overwriting save() on the model: > > def save(self): > if not self.position: > self.position = self.objects.count() + 1 > super(YourClassName, self).save() > > -- > DR. Couldn' t why do you need a function with a parameter? Wouldn't a function like this work: class Widget(models.Model): ... some fields position = models.PositiveIntegerField(default=getNextPosition) def getNextPosition(): return Widget.objects.count() + 1 Thanks, John --~--~-~--~~~---~--~~ 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?hl=en -~--~~~~--~~--~--~---
Re: Pre-populating an integer field with a count of model objects
Hi Daniel, Thanks for the reply. I'll just go with overriding the save. Cheers, Brandon On Aug 21, 11:29 am, Daniel Roseman <[EMAIL PROTECTED]> wrote: > On Aug 21, 4:42 pm, Brandon Taylor <[EMAIL PROTECTED]> wrote: > > > Hi everyone, > > > I have a column, 'position', which is a PositiveIntegerField, to allow > > my end-user to order records with. I would like to pre-populate the > > field when creating a new record, with the count of the model objects > > + 1. > > > The models documentation says 'default' can be a value or a callable, > > but using self.objects.count() + 1 doesn't work, because 'self' hasn't > > been defined. > > > Can anyone point me to an example of how I can accomplish this? > > > TIA, > > Brandon > > 'self.objects.count() + 1' isn't a callable, it's an expression, which > you can't use here. (A callable is a function object, or a class with > __call__ defined.) > However, unfortunately even a callable probably wouldn't help you > here, as the implementation calls it without any parameters. It's > really just for things like inserting the current time, which doesn't > need parameters. > Seehttp://www.djangoproject.com/documentation/models/field_defaults/ > > You can achieve what you want by overwriting save() on the model: > > def save(self): > if not self.position: > self.position = self.objects.count() + 1 > super(YourClassName, self).save() > > -- > DR. --~--~-~--~~~---~--~~ 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?hl=en -~--~~~~--~~--~--~---
Re: Pre-populating an integer field with a count of model objects
On Aug 21, 4:42 pm, Brandon Taylor <[EMAIL PROTECTED]> wrote: > Hi everyone, > > I have a column, 'position', which is a PositiveIntegerField, to allow > my end-user to order records with. I would like to pre-populate the > field when creating a new record, with the count of the model objects > + 1. > > The models documentation says 'default' can be a value or a callable, > but using self.objects.count() + 1 doesn't work, because 'self' hasn't > been defined. > > Can anyone point me to an example of how I can accomplish this? > > TIA, > Brandon 'self.objects.count() + 1' isn't a callable, it's an expression, which you can't use here. (A callable is a function object, or a class with __call__ defined.) However, unfortunately even a callable probably wouldn't help you here, as the implementation calls it without any parameters. It's really just for things like inserting the current time, which doesn't need parameters. See http://www.djangoproject.com/documentation/models/field_defaults/ You can achieve what you want by overwriting save() on the model: def save(self): if not self.position: self.position = self.objects.count() + 1 super(YourClassName, self).save() -- DR. --~--~-~--~~~---~--~~ 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?hl=en -~--~~~~--~~--~--~---