Commit 89857885 authored by Nikolai R Kristiansen's avatar Nikolai R Kristiansen
Browse files

select_related to the rescue, such performance!

parent 2a0744f6
......@@ -4,13 +4,12 @@ from django.forms import ModelChoiceField
from mdb.models import Interface, Ip4Address
class Ipv4ModelChoiceField(ModelChoiceField):
# def __init__(self, *args, **kwargs):
# super(Ipv4ModelChoiceField, self).__init__(*args, **kwargs)
class Ip4ModelChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
# TODO improve those labelZ
return obj.__str__()
if not hasattr(obj, 'interface'):
return obj.address
return "{} ({})".format(obj.address, obj.interface.host.hostname)
class InterfaceForm(forms.ModelForm):
......@@ -19,4 +18,7 @@ class InterfaceForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(InterfaceForm, self).__init__(*args, **kwargs)
self.fields['ip4address'] = Ipv4ModelChoiceField(queryset=Ip4Address.objects.select_related())
# Join Ip4Address with tables interface and host,
# before printing the select labels that walk those two relations.
queryset = Ip4Address.objects.select_related('interface', 'interface__host')
self.fields['ip4address'] = Ip4ModelChoiceField(queryset=queryset)
......@@ -472,12 +472,7 @@ class Ip4Address(models.Model):
ping_avg_rtt = models.FloatField(null=True, blank=True)
def __str__(self):
# FIXME: generates 2 SQL queries?
if not hasattr(self, 'interface'):
return self.address
# FIXME: Remove this when admin is overridden
return "{} ({})".format(self.address, self.interface.host.hostname)
return self.address
def assigned_to_host(self):
return self.interface.host
......
......@@ -76,6 +76,7 @@ INSTALLED_APPS = (
'django.contrib.admin',
'rest_framework',
'rest_framework.authtoken',
'django_extensions',
'mdb',
)
......
from django.core.wsgi import get_wsgi_application
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mdb.settings")
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mdbsite.settings")
application = get_wsgi_application()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment