Skip to content

Commit

Permalink
Merge 3932ae4 into f0b56c0
Browse files Browse the repository at this point in the history
  • Loading branch information
treyhunner committed Apr 9, 2013
2 parents f0b56c0 + 3932ae4 commit 704cc91
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions model_utils/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ def __init__(self, *args, **kwargs):
self.check_for_status = not kwargs.pop('no_check_for_status', False)
super(StatusField, self).__init__(*args, **kwargs)

def contribute_to_class(self, cls, name):
if not cls._meta.abstract and self.check_for_status:
assert hasattr(cls, 'STATUS'), \
def prepare_class(self, sender, **kwargs):
if not sender._meta.abstract and self.check_for_status:
assert hasattr(sender, 'STATUS'), \
"To use StatusField, the model '%s' must have a STATUS choices class attribute." \
% cls.__name__
self._choices = cls.STATUS
% sender.__name__
self._choices = sender.STATUS
if not self.has_default():
self.default = tuple(cls.STATUS)[0][0] # set first as default
self.default = tuple(sender.STATUS)[0][0] # set first as default

def contribute_to_class(self, cls, name):
models.signals.class_prepared.connect(self.prepare_class, sender=cls)
super(StatusField, self).contribute_to_class(cls, name)


Expand All @@ -81,9 +84,13 @@ def __init__(self, *args, **kwargs):
self.monitor = monitor
super(MonitorField, self).__init__(*args, **kwargs)

def prepare_class(self, sender, **kwargs):
self.monitor_attname = '_monitor_%s' % self.name
models.signals.post_init.connect(self._save_initial, sender=sender)

def contribute_to_class(self, cls, name):
self.monitor_attname = '_monitor_%s' % name
models.signals.post_init.connect(self._save_initial, sender=cls)
self.name = name
models.signals.class_prepared.connect(self.prepare_class, sender=cls)
super(MonitorField, self).contribute_to_class(cls, name)

def get_monitored_value(self, instance):
Expand Down

0 comments on commit 704cc91

Please sign in to comment.