On 10/04/2016 10:12 PM, Andrew Chiw wrote:
> |
> classClient(UserenaBaseProfile):
> Â Â def__str__(self):
> Â Â Â Â
returnself.name
>
> Â Â user =models.OneToOneField(User,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unique=True,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â verbose_name=_('Django
> Login'),
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â related_name='client')
>
> Â Â name =models.CharField(max_length=100,unique=True,default=
user.name)
In the save() method of the Client class you can populate Client.name
from Client.user.firstname plus Client.user.lastname
def save(self, *args, **kwargs):
if
self.id:
self.name = self.get_name()
super(Client, self).save(*args, **kwargs)
def get_name(self):
return "{0} {1}".format(self.user.firstname, self.user.lastname)
Alternatively, you could just create a name() method which does the same
as get_name() and you wouldn't then bother with the name field.
Also, it is possible that the Django User class has a get_name() method
which you can call as required. You need to check that.
Mike
>
> |
>
> The title says it all - this class is supposed to have a OneToOneField
> relationship with a Django User, and through django-toolbar I've seen
> that when a user signs up, the Django User is created first and then my
> Client class. But how do I get the Client class to have the same name as
> the Django User's name?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
django-users...@googlegroups.com
> <mailto:
django-users...@googlegroups.com>.
> To post to this group, send email to
django...@googlegroups.com
> <mailto:
django...@googlegroups.com>.
> Visit this group at
https://groups.google.com/group/django-users.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/django-users/cadbc069-1cbd-49e7-996c-fdb94adb537a%40googlegroups.com
> <
https://groups.google.com/d/msgid/django-users/cadbc069-1cbd-49e7-996c-fdb94adb537a%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit
https://groups.google.com/d/optout.