I have a model structure like this:
app1.models
class App(FunkyClassLoadoerMixin, DateMixin):
user = models.OneToOneField(User, blank=False, null=False, db_index=True, unique=True)
# some other fields
class DateMixin(models.Model):
date_added = models.DateTimeField(auto_now_add=True)
app2.models
from app1.models import App as BaseApp
class App(BaseApp)
class Meta:
proxy = True
FunkyClassLoadoerMixin is just a class that helps to load child classes in a slightly different way, but it doesn't affect the behaviour of them.
Given this, I have a query like:
q = SuperStatic.objects.all().select_related('user__app')
(in this case, the app object should be of type app2.models.App)
And when it is evaluated, I'm getting this error:
local/lib/python2.7/site-packages/django/db/models/query.pyc in get_cached_row(row, index_start, using, klass_info, offset)
1435 for rel_field, rel_model in rel_obj._meta.get_fields_with_model():
1436 if rel_model is not None:
-> 1437 setattr(rel_obj, rel_field.attname, getattr(obj, rel_field.attname))
1438 # populate the field cache for any related object
1439 # that has already been retrieved
AttributeError: 'User' object has no attribute 'date_added'
I am not really sure why this is happening. I had a look into Django source code and the reason seems to be that this function:
rel_obj._meta.get_fields_with_model()
is behaving differently when a model is a subclass and ignoring the fact it is a Proxy model.
I found some related post:
- https://code.djangoproject.com/ticket/10955
- http://python.6.x6.nabble.com/Django-17876-get-klass-info-should-handle-proxy-models-and-with-select-related-td4569828.html
But didn't help much neither.
Any ideas about how to use use the proxy class and avoid this error?