简体   繁体   中英

Extend User Model from Django?

How I create new Users:

from twittexApp.forms import UserCreationForm
from django.views.generic import CreateView
from twittexApp.models import RegUser, User

class RegisterView(CreateView):
    template_name = 'register.html'
    model = RegUser
    success_url = '/login/'
    form_class = UserCreationForm

My extended User Model:

from django.db import models
from django.contrib.auth.models import User

    class RegUser(models.Model):
        user = models.OneToOneField(User)
        description = models.CharField(max_length = 140, default="foo")

My question is how do I get the attribute "description" from an user?

I already know that I can get the "standard" user model via

<h1>My Profile</h1>
<ul>
    <div>{{ request.user.username }}</div>
</ul>

But getting the description doesnt work. I tried 2 different versions but it doesnt show "foo".

<h1>My Profile</h1>
<ul>
    <div>{{ request.user.desciption }}</div>
    <div>{{ request.user.reguser.desciption }}</div>
</ul>

Maybe is a fool answer but you have a miss spell.

Change:

{{ request.user.desciption }}

For this:

{{ request.user.description }}
{{ request.user.reguser.description }}

You can access "reverse" side of the relation. In this case user.reguser . Also check, if user has related RegUser instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM