from django.contrib import admin
from main.models import Building, Property

class PropertyInline(admin.TabularInline):
    model = Property

class BuildingAdmin(admin.ModelAdmin):
    inlines = [PropertyInline]
    def get_list_display(self, request):
        result = ('name', 'property_count')
        if request.user.is_superuser:
            result = ('user',) + result
        return result
    def get_queryset(self, request):
        qs = super(BuildingAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(user=request.user)
    def get_form(self, request, obj=None, **kwargs):
        self.exclude = []
        if not request.user.is_superuser:
            self.exclude.append('user')
        return super(BuildingAdmin, self).get_form(request, obj, **kwargs)
    def save_model(self, request, obj, form, change):
        if not request.user.is_superuser:
            obj.user = request.user
        obj.save()

class PropertyAdmin(admin.ModelAdmin):
    def get_list_display(self, request):
        result = ('name', 'address', 'building')
        if request.user.is_superuser:
            result = ('user',) + result
        return result
    def get_queryset(self, request):
        qs = super(PropertyAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(user=request.user)
    def get_form(self, request, obj=None, **kwargs):
        self.exclude = []
        if not request.user.is_superuser:
            self.exclude.append('user')
        return super(PropertyAdmin, self).get_form(request, obj, **kwargs)
    def save_model(self, request, obj, form, change):
        if not request.user.is_superuser:
            obj.user = request.user
        obj.save()

admin.site.register(Building, BuildingAdmin)
admin.site.register(Property, PropertyAdmin)


@@ -74,73 +74,8 @@
 ty%0A%0A
-class PropertyInline(admin.TabularInline):%0A    model = Property%0A%0A
 clas
@@ -113,39 +113,8 @@
 n):%0A
-    inlines = %5BPropertyInline%5D%0A
     
@@ -1106,32 +1106,313 @@
   return result%0A
+    def formfield_for_foreignkey(self, db_field, request, **kwargs):%0A        if db_field.name == %22building%22:%0A            kwargs%5B%22queryset%22%5D = Building.objects.filter(user=request.user)%0A        return super(PropertyAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)%0A
     def get_quer

Remove inline editor. Filter building foreign key