注意:
我只是想让我的应用程序显示在管理区域中,但它无法正常工作。我已经在这个问题上阅读了本网站其他地方的其他问题/答案,但没有一个能帮助我实现这一目标。我猜测问题在于我是如何导入模型的,但对我来说看起来并不错......
exp / settings.py 的相关部分:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'thingy',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
EXP /啄/ admin.py :
from thingy.models import Daily
from django.contrib import admin
class Daily(admin.ModelAdmin):
fields = ['user', 'hours_as_sec']
admin.site.register(Daily)
EXP /啄/ models.py :
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Daily(models.Model):
user=models.ForeignKey(User, unique=True)
day = models.DateField()
hours_as_sec = models.PositiveIntegerField()
notes = models.TextField()
class Monthly(models.Model):
user=models.ForeignKey(User, unique=True)
month = models.DateField()
hours_as_sec = models.PositiveIntegerField()
notes = models.TextField()
class WorkedWith(models.Model):
user=models.ForeignKey(User, unique=True)
day = models.DateField()
hours_as_sec = models.PositiveIntegerField()
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
url = models.URLField("Website", blank=True)
company = models.CharField(max_length=50, blank=True)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
我很无能为力。有任何想法吗?我让这个工作in the official tutorial但似乎无法让它在任何自制的应用程序中工作,所以我知道我做错了但我不知道是什么。
答案 0 :(得分:4)
在 admin.py 中,您应该更改注册模型的方式。 从
更改班级名称class Daily(admin.ModelAdmin):
fields = ['user', 'hours_as_sec']
到
class DailyAdmin(admin.ModelAdmin):
fields = ['user', 'hours_as_sec']
并注册如下:
admin.site.register(Daily,DailyAdmin)