I am new to Django and I have simple model like this.
from django.db import models
from django.contrib.auth.models import User
class Todo(models.Model):
owner = models.ForeignKey(User)
description = models.CharField(max_length=30)
done = models.BooleanField()
updated = models.DateTimeField(auto_now_add=True)
class News(models.Model):
title = models.TextField(max_length=400000)
description = models.TextField(max_length=400000)
created = models.DateTimeField(editable=False)
modified = models.DateTimeField()
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = timezone.now()
self.modified = timezone.now()
return super(User, self).save(*args, **kwargs)
In url,
from django.conf.urls import patterns, include, url
from todo import views
from rest_framework import viewsets, routers
from django.contrib import admin
router = routers.DefaultRouter()
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', admin.site.urls),
url(r'^', include(router.urls)),
)
I check in stackoverflow and they say autodiscover should be removed. But if I remove, I see like this. How shall I do so that my model is shown in admin?
You must register model in admin app
see https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#modeladmin-objects
in your admin.py file do this
from django.contrib import admin
from .models import Todo, News
# Register your models here.
admin.site.register(Todo)
admin.site.register(News)
Your app is probably not registered in INSTALLED_APPS
, if it dosent' find your app it fails without giving an error
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.