Django的版本:1.8.4 Python版本:2.7.10 我的Django模型像這樣: 樣/ models.pyDjango管理對象添加字段錯誤
from django.db import models
class Kind(models.Model):
id = models.IntegerField(null=False, primary_key=True)
name = models.CharField(max_length=64, null=False, default="default kind name")
create_time = models.DateField(auto_now=True, auto_created=True)
遊戲/ models.py
from django.db import models
from kind import models as kind_models
class Game(models.Model):
id = models.IntegerField(primary_key=True)
kind = models.ForeignKey(kind_models.Kind)
name = models.CharField(max_length=64, null=False, default="default game")
description = models.TextField(null=False, default="default description")
create_date = models.DateField(auto_created=True, auto_now=True)
sale_date = models.DateField()
遊戲/ amdin.py
from django.contrib import admin
from game import models as game_models
class AuthorAdmin(admin.ModelAdmin):
list_display = ('id', 'get_kind', 'name', 'description', 'create_date', 'sale_date')
search_fields = ('id', 'get_kind', 'name', 'description', 'create_date', 'sale_date')
list_filter = ('kind__name', 'name', 'create_date', 'sale_date')
ordering = ('id',)
fields = ('id', 'get_kind', 'name', 'description', 'sale_date')
def get_kind(self, obj):
return obj.kind.name
get_kind.short_description = 'kind'
get_kind.empty_value_display = 'not value set'
admin.site.register(game_models.Game, AuthorAdmin)
我跑我的APPLICA併成功查看管理員,而不會有任何錯誤或警告。但是,當我點擊add game
,網頁給我一些錯誤這樣的:
FieldError at /admin/game/game/add/
Unknown field(s) (get_kind) specified for Game. Check fields/fieldsets/exclude attributes of class AuthorAdmin.
Request Method: GET
Request URL: http://0.0.0.0:8000/admin/game/game/add/
Django Version: 1.8.4
Exception Type: FieldError
Exception Value:
Unknown field(s) (get_kind) specified for Game. Check fields/fieldsets/exclude attributes of class AuthorAdmin.
其實,我並不擅長Django的,我只是一個Django初學者。
我設置了'fields =(「kind」)',但是當我添加一個遊戲記錄時,它顯示了很多字符串值'kind object'。我該怎麼辦? –
你應該在'Kind'模型中指定'__str__'方法(或者python 2.7中的'__unicode__'),它將返回有意義的字符串(例如kind的名字)。 – GwynBleidD
特別感謝!它的工作。 –