这是模型:
author=models.ManyToManyField(Author)
模型管理员:
filter_horizontal=("author",)
它仍在呈现多选小部件。我不知道有什么问题。
姜戈 1.5。
mysite/mysite/views.py
from django.shortcuts import render_to_response
from django.http import HttpResponse,Http404
import datetime
def hello(request):
return HttpResponse("Hello world")
def current_time(request):
now=datetime.datetime.now()
return render_to_response('current_datetime.html',{'current_date':now})#(template,context) ADDED BY ROBERT
def hours_ahead(request,offset):
try:
offset=int(offset)
except ValueError:
raise Http404()
dt=datetime.datetime.now()+datetime.timedelta(hours=offset)
html='In %d hours, it will be %s.'%(offset,dt)
return HttpResponse(html)
def display_meta(request):
values=request.META.items()
values.sort()
html=[]
for k,v in values:
html.append('<tr><td>%s</td><td>%s</td></tr>'%(k,v))
return HttpResponse('<table>%s</table>' % '\n'.join(html))
mysite/mysite/urls.py
from django.conf.urls import patterns, include, url
from mysite.views import *
#from books import views
from books import views
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
('^hello/$',hello),
('^time/$',current_time),
(r'^time/(\d{1,2})/$',hours_ahead),
(r'^admin/',include(admin.site.urls)),
(r'^display_meta/$',display_meta),
(r'^search/$',views.search),
)
我的网站/书籍/models.py
from django.db import models
from django.contrib import admin
# Create your models here.
class Publisher(models.Model):
name=models.CharField(max_length=30)
address=models.CharField(max_length=50)
city=models.CharField(max_length=60)
state_province=models.CharField(max_length=30)
country=models.CharField(max_length=30)
website=models.URLField()
def __unicode__(self):
return self.name
class Author(models.Model):
first_name=models.CharField(max_length=30)
last_name=models.CharField(max_length=30)
email=models.EmailField(blank=True,verbose_name='e-mail')
def __unicode__(self):
return u'%s %s' %(self.first_name,self.last_name)
class AuthorAdmin(admin.ModelAdmin):
list_display=('first_name','last_name','email')
search_fields=('first_name','last_name')
class Book(models.Model):
name=models.CharField(max_length=100)
author=models.ManyToManyField(Author)
publisher=models.ForeignKey(Publisher)
publication_date=models.DateField(null=True,blank=True,verbose_name='Publication Date')
def __unicode__(self):
return u'%s' % self.name
class BookAdmin(admin.ModelAdmin):
list_display=('name','publisher','publication_date')
list_filter=('publication_date',)
ordering=('-publication_date',)
date_hierachy='publication_date'
#fields=('name','author','publisher','publication_date')
filter_horizontal=('author',)
管理员.py
from django.contrib import admin
from books.models import *
admin.site.register(Publisher)
admin.site.register(Author,AuthorAdmin)
admin.site.register(Book,BookAdmin)