Commit 2a0744f6 authored by Nikolai Kristiansen's avatar Nikolai Kristiansen
Browse files

Override form in InterfaceInline to get to expensive lookup

parent 15cde2cc
from django.contrib import admin, messages
from mdb.forms import InterfaceForm
from mdb.models import Ip6Address, Interface, Ip4Address, DhcpOption, DhcpCustomField, DomainSrvRecord, DomainTxtRecord, \
DomainCnameRecord, DomainARecord, Domain, Host, Ip4Subnet, Ip6Subnet, Nameserver, MailExchange, OperatingSystem, \
HostType, DhcpConfig, DomainAAAARecord
......@@ -14,6 +14,7 @@ class InterfaceInline(admin.TabularInline):
inlines = [Ip6AddressInline]
model = Interface
extra = 0
form = InterfaceForm
def get_queryset(self, request):
return super(InterfaceInline, self).get_queryset(request).select_related()
......
from django import forms
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)
def label_from_instance(self, obj):
# TODO improve those labelZ
return obj.__str__()
class InterfaceForm(forms.ModelForm):
model = Interface
def __init__(self, *args, **kwargs):
super(InterfaceForm, self).__init__(*args, **kwargs)
self.fields['ip4address'] = Ipv4ModelChoiceField(queryset=Ip4Address.objects.select_related())
......@@ -476,6 +476,7 @@ class Ip4Address(models.Model):
if not hasattr(self, 'interface'):
return self.address
# FIXME: Remove this when admin is overridden
return "{} ({})".format(self.address, self.interface.host.hostname)
def assigned_to_host(self):
......
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