On Oct 26, 4:22 pm, Alessandro Ronchi <[email protected]>
wrote:
> I need to create a static method for a model class like this. The line
> Settore.objects.all()[0].id gives error (
> psycopg2.InternalError: current transaction is aborted) when creating
> the tables at first syncdb. What's the correct way to do that?
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~
> class Settore(models.Model):
> nome = models.CharField(max_length=200, blank=False, null=False)
> email = models.EmailField()
>
> class Meta:
>
> verbose_name_plural = 'settori'
> ordering = ('nome',)
> get_latest_by = "nome"
>
> def __unicode__(self):
> return self.nome
>
> @staticmethod
> def get_default():
> try:
> return Settore.objects.all()[0].id
> except:
> return None
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> --
> Alessandro Ronchi
Are you by any chance using that get_default method as the default for
a model field somewhere else? If so, have you perhaps done it like
this:
myfield = models.ForeignKey(Settore, default=Settore.get_default
())
This would call the get_default() staticmethod when the model is
defined, rather than when a new instance is defined. You need to pass
the callable itself:
myfield = models.ForeignKey(Settore, default=Settore.get_default)
- notice the lack of calling () brackets.
All this said, I agree with Ethan that you should be doing this in a
Manager, although I suspect the error might still occur as long as you
call the method in the default, rather than passing the callable.
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---