Django model class and custom property
Posted
by dArignac
on Stack Overflow
See other posts from Stack Overflow
or by dArignac
Published on 2010-06-15T15:01:38Z
Indexed on
2010/06/15
15:22 UTC
Read the original article
Hit count: 188
Howdy - today a weird problem occured to me:
I have a modle class in Django and added a custom property to it that shall not be saved into the database and therefore is not represent in the models structure:
class Category(models.Model):
groups = models.ManyToManyField(Group)
title = defaultdict()
Now, when I'm within the shell or writing a test and I do the following:
c1 = Category.objects.create()
c1.title['de'] = 'german title'
print c1.title['de'] # prints "german title"
c2 = Category.objects.create()
print c2.title['de'] # prints "german title" <-- WTF?
It seems that 'title' is kind of global. If I change title to a simple string it works as expected, so it has to do something with the dict? I also tried setting title as a property:
title = property(_title)
But that did not work, too. So, how can I solve this? Thank you in advance!
enter code here
© Stack Overflow or respective owner