Note that there are some explanatory texts on larger screens.

plurals
  1. POConfiguring Many-to-many field in django admin with related_name
    primarykey
    data
    text
    <p>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 option in the ManyToMany field to be able to get the collection of vlan from the related subnet object. Adding subnet to vlans from the admin works well. However when I try to add an horizontal_filer to the subnet admin in order to add van to its vlan set I get an error saying that the vlans attribute doesn't exist. I'm using subnet object in some view and I can access the vlans attribute just right.</p> <p>What am I doing wrong here ? I've seen similar post but I couldn't adapt any of the proposed solution with success.</p> <p>Thanks for your help</p> <p>model.py</p> <pre><code>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) </code></pre> <p>admin.py </p> <pre><code>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) </code></pre> <p>and the error I get</p> <pre><code>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'. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload