Using Mixins to add common fields to a model doesn't work
stuaxo opened this issue · comments
I tried using a mixin with some common fields, but it doesn't work:
class CeleryTaskMixin:
celery_status = models.CharField() # last known status, call update_status
celery_id = models.CharField() # celery uuid
f_name = models.CharField(indexed=True)
class RunningTask(CeleryTaskMixin, models.Model):
def __init__(self, t=None, *args, **kwargs):
CeleryTaskMixin.__init__(self)
models.Model.__init__(self)
task_type = models.CharField()
start_time = models.DateTimeField(auto_now=True)
end_time = models.DateTimeField()
Then when I try and do
models.RunningTask.objects.filter(f_name="blah")
It says
File "/home/stu/.virtualenvs/ng/src/redisco/redisco/models/modelset.py", line 52, in __iter__
for id in self._set:
File "/home/stu/.virtualenvs/ng/src/redisco/redisco/models/modelset.py", line 294, in _set
s = self._add_set_filter(s)
File "/home/stu/.virtualenvs/ng/src/redisco/redisco/models/modelset.py", line 317, in _add_set_filter
(k, self.model_class.__name__))
redisco.models.exceptions.AttributeNotIndexed: Attribute f_name is not indexed in RunningTask class.
This seems like something that should work... (?)
Ah, I made the baseclass extend the Model as well and things seem to work ... but does that mean I now have two models in the database ?
DOH - I was initializing the Model constructor like models.Model.__init__(self)
not models.Model.__init__(self, *args, **kwargs)