2

这是模型:

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)
4

1 回答 1

6

我认为现在回答为时已晚;但是,我在阅读 django-core 书籍时遇到了同样的问题,我认为这可能是您问题的来源:

只需在隐身模式或其他浏览器中打开管理面板,您还可以通过从浏览器中删除缓存的内容来解决问题。我认为问题可能源于在您进行更改之前第一次不完整加载 javascript。然而; 以隐身私人模式打开它为我解决了这个问题。

于 2017-04-17T20:38:05.403 回答