我在 djando 1.5 中定义了以下模型和管理员。这是子网和vlan之间的多对多关系。我在 ManyToMany 字段中使用 related_name 选项,以便能够从相关子网对象中获取 vlan 的集合。从管理员向 vlan 添加子网效果很好。但是,当我尝试将 Horizontal_filer 添加到子网管理员以便将 van 添加到其 vlan 集中时,我收到一条错误消息,指出 vlans 属性不存在。我在某些视图中使用了子网对象,并且可以正确访问 vlans 属性。
我在这里做错了什么?我看过类似的帖子,但我无法成功调整任何提议的解决方案。
谢谢你的帮助
模型.py
from django.db import models
class Subnet(models.Model):
networkAddress = models.CharField(max_length=15)
size = models.IntegerField()
def __unicode__(self):
return "%s/%s" % (self.networkAddress, self.size)
class IpAddress(models.Model):
ipAddress = models.CharField(max_length=15)
subnet = models.ForeignKey(Subnet)
def __unicode__(self):
return "%s" % (self.ipAddress)
class Vlan(models.Model):
number = models.IntegerField()
description = models.CharField(max_length=150)
subnets = models.ManyToManyField(Subnet, related_name='vlans', blank=True)
def __unicode__(self):
return "VLAN %s (%s)" % (self.number, self.description)
管理员.py
from network.models import Subnet, IpAddress, Vlan
from django.contrib import admin
class SubnetAdmin(admin.ModelAdmin):
filter_horizontal = ('vlans',)
admin.site.register(Subnet, SubnetAdmin)
admin.site.register(IpAddress)
admin.site.register(Vlan)
和我得到的错误
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.5.2
Exception Type: ImproperlyConfigured
Exception Value:
'SubnetAdmin.filter_horizontal' refers to field 'vlans' that is missing from model 'network.Subnet'.