如何在Django中的UserProfile中查看(相同的)User(User)数据

时间:2015-10-20 17:03:15

标签: python django authentication django-admin

所以我遇到了这种情况。

-models.py

@(Html.Kendo().DropDownListFor(model=>model.CustomerGroup)

-forms.py

from django.db import models
from django.utils import timezone
from autoslug import AutoSlugField
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    website = models.URLField(blank=True)
    picture = models.ImageField(upload_to='profile_images', blank=True)

    def __unicode__(self):
        return self.user.username

-views.py

from django import forms
from django.forms import ModelForm
from tentagarden.models import UserProfile
from django.contrib.auth.models import User
from parsley.decorators import parsleyfy

@parsleyfy
class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password')

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('website', 'picture')

-admin.py

from django.shortcuts import render_to_response, render, redirect
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required

from tentagarden.forms import UserForm, UserProfileForm

def home(request):
    context = RequestContext(request)

    registered = False

    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()

            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'picture' in request.FILES:
                profile.picture = request.FILES['picture']

            profile.save()

            registered = True

        else:
            print user_form.errors, profile_form.errors

    else:
        user_form = UserForm()
        profile_form = UserProfileForm()

    return render_to_response(
            'tentagarden/home.html',
            {'user_form': user_form, 'profile_form': profile_form, 'registered': registered},
            context)

我想在"用户个人资料"中显示用户数据(所以名字,姓氏和电子邮件)。 (在管理员页面中),而不是"用户"只。
我该怎么办?

谢谢!

1 个答案:

答案 0 :(得分:1)

把它放在你的admin.py

class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('email', 'first_name', 'last_name', 'website')
    search_fields = ('user__username', 'user__first_name', 'user__last_name', 'user__email')
    # list_filter = ('some_field_to_filter')
    readonly_fields = ('email', 'first_name', 'last_name')

    def email(self, obj):
        return obj.user.email
    def first_name(self, obj):
        return obj.user.first_name
    def last_name(self, obj):
        return obj.user.last_name

admin.site.register(UserProfile, UserProfileAdmin)

使用fieldsets控制编辑视图中显示字段的顺序。