Override form in InterfaceInline to get to expensive lookup

This commit is contained in:
nikolark committed 2016-01-06 22:39:32 +01:00
1 parent 15cde2cce4
commit 2a0744f602
3 files changed
+25 -1

No files matched your search

+2 -1
View File
@@ -1,5 +1,5 @@
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()
+22
View File
@@ -0,0 +1,22 @@
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())
+1
View File
@@ -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):