我在djando 1.5中定义了以下模型和管理员。这是子网和vlan之间的多对多关系。我在ManyToMany字段中使用related_name选项,以便能够从相关的子网对象中获取vlan的集合。从管理员向vlan添加子网效果很好。但是,当我尝试将horizontal_filer添加到子网admin以便将van添加到其vlan集时,我收到一条错误消息,指出vlans属性不存在。我在某些视图中使用子网对象,我可以正确访问vlans属性。
我在这里做错了什么?我见过类似的帖子,但我无法适应任何提议的解决方案。
感谢您的帮助
model.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)
admin.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'.
答案 0 :(得分:7)
显然这是8 year old feature request。有django-admin-extend。或者你可以抛出一些东西like this in there:
from django.contrib import admin as admin_module
class SiteForm(ModelForm):
user_profiles = forms.ModelMultipleChoiceField(
label='Users granted access',
queryset=UserProfile.objects.all(),
required=False,
help_text='Admin users (who can access everything) not listed separately',
widget=admin_module.widgets.FilteredSelectMultiple('user profiles', False))
class SiteAdmin(admin_module.ModelAdmin):
fields = ('user_profiles',)
def save_model(self, request, obj, form, change):
# save without m2m field (can't save them until obj has id)
super(SiteAdmin, self).save_model(request, obj, form, change)
# if that worked, deal with m2m field
obj.user_profiles.clear()
for user_profile in form.cleaned_data['user_profiles']:
obj.user_profiles.add(user_profile)
def get_form(self, request, obj=None, **kwargs):
if obj:
self.form.base_fields['user_profiles'].initial = [ o.pk for o in obj.userprofile_set.all() ]
else:
self.form.base_fields['user_profiles'].initial = []
return super(SiteAdmin, self).get_form(request, obj, **kwargs)
当您在filter_horizontal
元组中指定时,它应该为您提供fields
。
答案 1 :(得分:0)
我创建了一个涵盖这个特定问题的公开要点。
https://gist.github.com/Wtower/0b181cc06f816e4feac14e7c0aa2e9d0
一般的想法是使用该特定的基本表单类,以便为表单定义“反向”m2m字段,否则将不包含它。然后轻松覆盖管理类中的form
。
尽管代码并不复杂,但要点代码在答案中包含的内容很长,所以请为此道歉。