在Django的管理站点中显示模型

时间:2016-05-19 10:10:28

标签: python django

我有一个模型,我希望在管理员网站上显示。

models.py:

class UserDetails(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='userdetails')
    address = models.CharField(max_length=50, default='Not set')

def __unicode__(self):
    return u' %s %s ' % (self.user, self.address)

admin.py:

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from index.models import User, UserDetails

class UserDetailsAdmin(admin.ModelAdmin):
    fields = ("address",)

admin.site.unregister(User)
admin.site.register(User, UserDetailsAdmin)

我收到错误:

  

FieldError:为User指定的未知字段(地址)。检查字段/字段集/排除类UserDetailsAdmin的属性。

出了什么问题?

2 个答案:

答案 0 :(得分:4)

admin.py

中的

这样做get details of your model attribute

class UserDetailsAdmin(admin.ModelAdmin):
    list_display = ('address', 'user')

admin.site.register(UserDetails, UserDetailsAdmin)

list_display控制fields

更改列表页面上displayed admin Sub CopyPaste() time1 = Timer Application.ScreenUpdating = False Application.Calculation = xlCalculationManual *your code here* Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub

答案 1 :(得分:2)

admin.py

更改

admin.site.register(User, UserDetailsAdmin)
admin.site.unregister(User)

admin.site.register(UserDetails, UserDetailsAdmin)
admin.site.register(User)