On Thu, Jul 14, 2011 at 2:09 PM, tom <thomas.st...@gmail.com> wrote:
> Hello,
>
> I have following proxy model
>
> class UserForSubdomain(User):
>    """
>    This Model acts as a proxy to the django model:``User`` and
>    allows to store the subdomain with the username to support
>    the same usernames with different subdomains.
>    The username is split with the first '.'. The first part is the
>    subdomain, everything that follows is the username.
>    """
>    class Meta:
>        proxy = True
>
>    def get_username(self):
>        domain = u_name = None
>        try:
>            domain, u_name = self.username.split('.', 1)
>        except ValueError:
>            #return the username if there is no . in the username
>            u_name = self.username
>        logger.debug('Full username for id %s is "%s". Split is:
> "%s"."%s"' % (self.id, self.username, domain, u_name))
>        return u_name
>
>    def __unicode__(self):
>        if self.first_name and self.last_name:
>            return self.get_full_name()
>        else:
>            return self.get_username()
>
> When I cast a ``User`` object to a ``UserForSubdomain`` object,
> somehow the values of the properties are not available anymore. I do
> this
>
>>>> user = User.objects.get(username = 'xyz.tstagl')
>>>> print user
> xyz.tstagl
>>>> s_user = UserForSubdomain(user)
>>>> print s_user
>
>>>> print s_user.get_username()
>
>>>>
>
> Has someone an idea what I am doing wrong here? I think I am missing
> something obvious, but I can't find out what.
>
> Many thanks for your help!
>
> regards, tom
>

You're missing that there is no such thing as casting in python.
"UserForSubdomain(user)" attempts to construct a new UserForSubdomain
object by calling __init__ with a User object as the first positional
parameter.

If you want a UserForSubdomain from a User, the simplest way is to
fetch it from the database, using the user id.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to