Am Sonntag, 6. Januar 2008 13:04:38 schrieb Brot:
> I have a few models which have the same two fields. Based on these
> fields there is always the same function in these models. But this
> violates DRY. Is there another possibility for my function/models
>
> [...]
>
> Model Inheritance isn't supported yet. Is there another way to avoid
> the duplicated code/function?

if you want to reduce the amount of duplicated code, simply create an abstract 
class and inherit that in your models like this:

class ValidFromTo(object):
    def is_active(self):
        return self.valid_from < datetime.now() < self.valid_to
    is_active.short_description = 'Active'
    is_active.boolean = True

class EventType(models.Model, ValidFromTo):
    ....
    valid_from = models.DateTimeField(default=datetime.now)
    valid_to = models.DateTimeField(default=DEF_VALID_TO)
    ......

class Event(models.Model, ValidFromTo):
    ....
    type = models.ForeignKey(EventType,
                             limit_choices_to =
                                    {'valid_from__lte': datetime.now,
                                     'valid_to__gte': datetime.now})
    valid_from = models.DateTimeField(default=datetime.now)
    valid_to = models.DateTimeField(default=DEF_VALID_TO)
    ......

class Partner(models.Model, ValidFromTo):
    ....
    valid_from = models.DateTimeField(default=datetime.now)
    valid_to = models.DateTimeField(default=DEF_VALID_TO)
    ......

not perfect yet since you still have to add the fields redundantly, but it's a 
first step ;)
-- 
MfG

        Nikl

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to