简体   繁体   中英

App (or Model) Not Showing up in Django Admin

Note:

  • Django 1.3
  • thingy is my experimental app's name.
  • been learning Python & Django for a week.

I'm just trying to get my app to show up in the admin area, but it won't work. I've already read other questions/answers elsewhere on this site on this issue, but none of it helped me get this working. I'm guessing the problem is in how I'm importing the model but it doesn't look wrong to me...

exp/settings.py 's relevant section:

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/thingy/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/thingy/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])

I'm clueless. Any ideas? I got this to work in the official tutorial but cannot seem to get it to work in any self-made app, so I know I'm doin something wrong but I don't know what.

In your admin.py you should change the way you're registering your model. Change the class name from

class Daily(admin.ModelAdmin):
    fields    = ['user', 'hours_as_sec']

to

class DailyAdmin(admin.ModelAdmin):
    fields    = ['user', 'hours_as_sec']

and register as follows:

admin.site.register(Daily,DailyAdmin)

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