from wndb.models import * from django.contrib import admin from django.db.models import get_models, get_app from django.contrib.admin.sites import AlreadyRegistered from django import forms from django.db.models import Q class InterfaceInline(admin.TabularInline): extra = 3 model = Interface class NodeAdmin(admin.ModelAdmin): inlines = [ InterfaceInline, ] admin.site.register(Node,NodeAdmin) class LinkForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(LinkForm, self).__init__(*args, **kwargs) if 'initial' in kwargs: self.fields['ip_block'].queryset = IPBlock.objects.filter(Q(link__isnull=True)) else: self.fields['ip_block'].queryset = IPBlock.objects.filter(Q(link__isnull=True)|Q(pk=self.instance.ip_block.id)) class LinkAdmin(admin.ModelAdmin): form = LinkForm admin.site.register(Link,LinkAdmin) for model in get_models(get_app('wndb')): try: admin.site.register(model) except AlreadyRegistered: pass