Re: How to add an element of derived data to a Django model?
On Saturday, January 5, 2013 8:48:00 AM UTC-8, Saqib Ali wrote:
I have a Django Model as follows:class myModel(models.Model):a = models.IntegerField()b = models.IntegerField()I want to add a models.BooleanField() named c to the myModel class. However the value of c is simply derived from a and b so I don't want it to be stored in the Database.I want to c to be True if a > b, otherwise False. I want c to reflect the correct/current value even when a and b get modifed. And I want to be able to access c using myModel.c just like I can access a and b.Is there any way to do this?
Use a property inside your class
@property
def c(self):
return self.a > self.b
Now you can access c as myModel.c anytime.
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/IITPrYviRhUJ.
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home