views:

26

answers:

1

I have the same conundrum as presented in this question, but applied to Django's auth.User.

I have this proxy model:

class OrderedUser(User):
    def __unicode__(self):
        return self.get_full_name()

    class Meta: 
        proxy=True
        ordering=["first_name", "last_name"]

And some of my other models use an OrderedUser instead of a django.contrib.auth.models.User as field types.

In my views I then use the request.user to populate a field and - as expected - get an error:

'Cannot assign "<User...>": <field> must be a "OrderedUser" instance'

I could just do OrderedUser.objects.get(request.user.id), but that is an extra hit to the database.

So, how to convert a base model class into its proxied class?

+1  A: 

It's another database hit, but this will work:

OrderedUser.objects.get(pk=request.user.pk)

Edit You could try:

o = OrderedUser()
o.__dict__ = request.user.__dict__
Daniel Roseman
Right. Is there a mechanism that doesn't involve a hit to the DB?
celopes
Yep, copying the dict did the trick. Thanks Daniel.
celopes