Django1.4,在admin已经打开的前提下,新建立的模块希望自动加入到后台管理当中:
1.在app下新建admin.py,假设app名为jobs,内部新建了两个表Locaion, Job,内容如下:
from django.db import models
# Create your models here.
class Location(models.Model):
city = models.CharField(max_length=50)
state = models.CharField(max_length=50, null=True, blank=True)
country = models.CharField(max_length=50)
class Admin:
list_display = ("city", "state", "country")
def __str__(self):
if self.state:
return "%s, %s, %s" % (self.city, self.state, self.country)
else:
return "%s, %s" % (self.city, self.country)
class Job(models.Model):
pub_date = models.DateField()
job_title = models.CharField(max_length=50)
job_description = models.TextField()
location = models.ForeignKey(Location)
def __str__(self):
return "%s (%s)" % (self.job_title, self.location)
则admin.py的内容为:
from jobs.models import Job
from jobs.models import Location
admin.site.register(Location)
admin.site.register(Job)
2.urls.py中需要打开admin自动扫描模式admin.autodiscover(),然后重新更新下数据库,重启即好。