Django disable editing (but allow adding) in TabularInline view

前端 未结 4 2237
小鲜肉
小鲜肉 2021-02-19 23:40

I want to disable editing ALL objects within a particular TabularInline instance, while still allowing additions and while still allowing editing of the parent model.

I

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-19 23:41

    See this solution: Django admin: make field editable in add but not edit

    Override get_readonly_fields method:

    def get_readonly_fields(self, request, obj=None):
        if obj: # obj is not None, so this is an edit
            return ['name1',..] # Return a list or tuple of readonly fields' names
        else: # This is an addition
            return []
    

提交回复
热议问题