2

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.

Forrest
  • 149
  • 1
  • 2
  • 13

1 Answers1

4

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)
kirbuchi
  • 2,274
  • 2
  • 23
  • 35
  • 2
    +1 it would be good to note (for others reading this) that the tutorial doesn't specify the `ModelAdmin` subclass to begin with and so the `admin.site.register(Model)` will work. – James Khoury Aug 01 '11 at 03:50
  • That's right, you'll only have to specify the ModelAdmin class if you need to customize the way your class is displayed on the admin site as shown here: https://docs.djangoproject.com/en/dev/intro/tutorial02/#customize-the-admin-form – kirbuchi Aug 01 '11 at 04:02
  • Changed it, cleared my cache, logged out & back in ...but it still ain't working. – Forrest Aug 01 '11 at 05:31
  • 2
    Wait, that fixed it... but only when I `python manage.py runserver`. It doesn't work if I view it via Apache. Strange... very strange. Thanks though, you've helped me a lot. Looks like I've got some other mysterious error to hunt down now. Typical. Solve one, get another or a couple nothers. – Forrest Aug 01 '11 at 05:46
  • Related reason why app might not show, for those using gunicorn: http://stackoverflow.com/questions/12773763/gunicorn-autoreload-on-source-change – Snorex Aug 25 '15 at 21:38