Last active
August 29, 2015 14:21
-
-
Save joshcartme/6856ee5e35c7a6456d9e to your computer and use it in GitHub Desktop.
Create and edit third level Django inlines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.admin.options import IS_POPUP_VAR | |
from django.template.response import SimpleTemplateResponse | |
from django.utils.html import escape, escapejs | |
from mezzanine.core.admin import TabularDynamicInlineAdmin | |
from mezzanine.pages.admin import PageAdmin | |
from .models import TopLevel, SecondLevel, ThirdLevel | |
class SecondLevelInline(TabularDynamicInlineAdmin): | |
model = SecondLevel | |
fields = [..., 'edit_third_level', '_order'] | |
readonly_fields = ['edit_third_level'] # edit_third_level must be in fields and readonly_fields or there will be an exception | |
class ThirdLevelInline(TabularDynamicInlineAdmin): | |
model = ThirdLevel | |
class SecondLevelAdmin(admin.ModelAdmin): | |
exclude = ['top_level', '_order'] | |
inlines = [ThirdLevelInline] | |
def in_menu(self): | |
""" | |
Hide from the admin menu unless explicitly set in ``ADMIN_MENU_ORDER``. | |
""" | |
for (name, items) in settings.ADMIN_MENU_ORDER: | |
if "theme.TopBarButton" in items: | |
return True | |
return False | |
def response_change(self, request, obj): | |
""" | |
Determines the HttpResponse for the change_view stage. | |
""" | |
pk_value = obj._get_pk_val() | |
if IS_POPUP_VAR in request.POST: | |
return SimpleTemplateResponse('admin/popup_response.html', { | |
'pk_value': escape(pk_value), | |
'obj': escapejs(obj) | |
}) | |
return super(GlossaryCategoryAdmin, self).response_change(request, obj) | |
admin.site.register(GlossaryCategory, GlossaryCategoryAdmin) | |
class TopLevelAdmin(PageAdmin): | |
inlines = [SecondLevelInline] | |
admin.site.register(TopLevel, TopLevelAdmin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mezzanine.pages.models import Page | |
from mezzanine.utils.urls import admin_url | |
class TopLevel(Page): | |
... | |
class SecondLevel(Orderable): | |
top_level = models.ForeignKey(TopLevel, related_name="categories") | |
... | |
def edit_third_level(self): | |
''' | |
Returns a link to this items change form | |
''' | |
if self.id: | |
return '<a onclick="return showAddAnotherPopup(this);" href="%s">Edit third level</a>' % admin_url(self.__class__, "change", self.id) | |
return '' | |
edit_third_level.allow_tags = True | |
class ThirdLevel(Orderable): | |
second_level = models.ForeignKey(SecondLevel) | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<!-- belongs in templates/admin/popup_response.html --> | |
<head><title></title></head> | |
<body> | |
<script type="text/javascript"> | |
try{ | |
opener.dismissAddAnotherPopup(window, "{{ pk_value }}", "{{ obj }}"); | |
} catch(e) { | |
if (e.name === 'ReferenceError') { | |
close(); | |
} else { | |
throw e; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment