Friday, January 28, 2011

use variable that is bound at an object's creation time at the object's creation time

Hello,

my model Product has an attribute spelling_variants and a property
variants:

class Product(models.Model):
canonical_name = models.CharField(max_length=200)
spelling_variants = models.ManyToManyField(String,
related_name="product spellings", blank=True)
...

@property
def variants(self):
variants = []
for v in self.spelling_variants:
variants += paraphrase(v)
return variants

The code works, if product instances are first saved without a vaue
for spelling_variants and later modified so as to have some spelling
variants. When I add spelling variants at the time when I create a
product in the admin interface, I get this error:

Caught TypeError while rendering: 'NoneType' object is not iterable

I think the problem in these cases is that when Python tries to
execute self.variants() it does not yet have a value for
self.spelling_variants.

How can I make sure that self.spelling_variants has a value when
self.variants() is executed?

Santiago

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment