Saturday, August 20, 2011

Model Inheritance and methods

I have the following models:

class Deal(models.Model):
"""
Deal is a deal for Interest. There should only be one
deal for each Interest pair
"""
interest = models.ForeignKey( Interest )
max_sell = models.IntegerField(default = 1)

def terms(self):
return self.term_set.all()

class Term( models.Model ):
"""
A Term are the terms of a Deal. There can be many Terms for each
Deal
"""
deal = models.ForeignKey( Deal )
canceled = models.BooleanField(default = False)
cost = models.CharField( max_length = 10, blank = True, null
= True )
buyer = models.ForeignKey( User, blank = True, null = True )

def execute(self, **kwargs):
pass

class Expire( Term ):
"""
Subclass of Term that is good until an expiration date
"""
date = models.DateField()

def execute(self):
if self.buyer == None or self.canceled:
return False

delta = self.date - date.today()
if delta.days >= 0:
return True
return False

def __unicode__(self, **kwargs):
return '<Expire:'+ self.date.strftime("%Y-%m-%d") + '>'

class Cancel( Term ):
"""
Subclass of Term that the Term is good until canceled
"""
def execute(self):
if self.buyer == None or self.canceled:
return False
return True

def __unicode__(self):
return '<Cancel>'


I want to do the following:
for term in deal.terms()
if term.execute():
do something


My thought was the term.execute method would be overridden and the
type of term, either cancel's or expire's execute method would occur,
but it only executes the term.execute()

I can get around it by having the following as the term execute, but
its ugly
def execute(self):
try:
term.cancel.execute()
except:
pass
try:
term.expire.execute()
except:
pass

Any suggestions?

--
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate