Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django admin readonly models

"""
source: https://archive.is/AiLh1
license: https://archive.is/iCoSO
"""


class ReadOnlyAdminMixin:
    """Disables all editing capabilities."""
    change_form_template = "admin/view.html"

    def __init__(self, *args, **kwargs):
        super(ReadOnlyAdminMixin, self).__init__(*args, **kwargs)
        self.readonly_fields = [f.name for f in self.model._meta.get_fields()]

    def get_actions(self, request):
        actions = super(ReadOnlyAdminMixin, self).get_actions(request)
        actions.pop("delete_selected", None)
        return actions

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def save_model(self, request, obj, form, change):
        pass

    def delete_model(self, request, obj):
        pass

    def save_related(self, request, form, formsets, change):
        pass

@register(models.Address)
class AddressAdmin(ReadOnlyAdminMixin, admin.ModelAdmin):
    list_display = ('country', 'state', 'city', 'zipcode')
    search_fields = ('country', 'state', 'city', 'zipcode')
    list_filter = ('country', 'state')
Comment

PREVIOUS NEXT
Code Example
Python :: split the column value and take first value in pandas 
Python :: google assistant in windows 10 
Python :: remove empty string from list python single line 
Python :: how to remove a list of numbers from a list in python 
Python :: python conditions 
Python :: plt dashed line 
Python :: get webpage python 
Python :: python find index of closest value in list 
Python :: python os.path.join 
Python :: Matplotlib add text to axes 
Python :: how to negate a boolean python 
Python :: check if any letter in string python 
Python :: pyqt set focus 
Python :: tic tac toe minimax 
Python :: Python - How To Check Operating System 
Python :: factorial program in python 
Python :: loop through list of lists jinja 
Python :: django sessions for beginners 
Python :: check if 2 strings are equal python 
Python :: split coumn of df into multiple dynamic columns 
Python :: python google docs api how to get doc index 
Python :: pass args and kwargs to funcitons 
Python :: pygame scroll event 
Python :: download python libraries offline 
Python :: How to Pass Additional Context into a Class Based View in django 
Python :: i have two versions of python installed mac 
Python :: how does works lamda in pyton 
Python :: tuple in python 3 
Python :: download youtube video 
Python :: python typing list of specific values 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =