I have a models.py file that looks like this:
from django.db import models
from common.models import Record
from tinymce import models as tinymce_models
# Create your models here.
class Address(Record):
def __unicode__(self):
return unicode(self.ip)
ip = models.IPAddressField(unique=True)
notes = models.TextField(blank=True)
class Meta:
verbose_name_plural = "Addresses"
class Servers(models.Model):
def __unicode__(self):
return unicode(self.server_name)
server_name = models.CharField(max_length=100)
ip_address = models.ForeignKey(Address)
secondary_ips = models.ManyToManyField(Address, verbose_name = 'Secondary IPs', blank=True, related_name='secondary_ips')
class Meta:
verbose_name_plural = "Servers"
I have a list of IP's and Servers in the system. I am trying to have the ManytoManyField only display the list of IP's that are not currently associated with a server.
I have the following queryset:
inner_qs = Servers.objects.values_list('ip_address_id', flat=True)
entries = Address.objects.exclude(id__in=inner_qs)
It returns only the IP addresses that are not in the Server table. I do not know how to incorporate those results into my ManytoManyField and where I am supposed to place my queryset. I currently only get the results when I enter the django Shell
Any ideas,
Thanks
If you want to add a queryset to a many to many field first change it into a list and add it as positional arguments using *
Example
# Returns a queryset
permissions = Permission.objects.all()
# Add the results to the many to many field (notice the *)
group = MyGroup.objects.get(name='test')
group.permissions.add(*permissions)
Returns a queryset
permissions = Permission.objects.all()
Add the results to the many to many field (notice the *
)
group = MyGroup.objects.get(name='test')
group.permissions.add(*permissions)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With