Configure many to many field in django admin with the appropriate name

I have the following model and admin defined in djando 1.5. This is a many-to-many relationship between subnet and vlan. I use the related_name parameter in the ManyToMany field to get the vlan assembly from the associated subnet object. Adding a subnet to vlans by admin works fine. However, when I try to add horizontal_filer to the subnet administrator in order to add van to my vlan set, I get an error message indicating that the vlans attribute does not exist. I am using a subnet object in some view and I can access the vlans attribute just right.

What am I doing wrong here? I saw a similar record, but I could not successfully adapt any proposed solution.

thanks for the help

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) 

and the error i get

 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'. 
+7
django django-models django-orm django-admin django-admin-filters
source share
2 answers

Apparently this is an 8 year feature request . There is django-admin-extend . Or you can just throw something like this 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) 

It should give you filter_horizontal when you specify it in the fields tuple.

+6
source share

I created a public entity that covers this particular issue.

https://gist.github.com/Wtower/0b181cc06f816e4feac14e7c0aa2e9d0

The general idea is to use this particular base form class to define the โ€œinverseโ€ m2m field for a form that would not otherwise include it. Then easily override form in the admin class.

Despite the fact that the code is not quite complicated, the gist code should somehow include in the response, so apologize for this.

0
source share

All Articles