1

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:

But didn't help much neither.

Any ideas about how to use use the proxy class and avoid this error?

danielmaxx
  • 353
  • 4
  • 12
  • The AttributeError mentions User model, not the App model. Did you test it without the app2.modles.App, i mean with the app1.models directly? Does it work?. Additionally, this guy says something about injecting fields when subclassing and working with proxies http://stackoverflow.com/a/7669063/344501 – jgomo3 Sep 21 '15 at 16:09

0 Answers0