33👍
You are to create admin.py in your module (probably book)
class BookAdmin(admin.ModelAdmin):
list_display = ("pk", "get_title_or_nothing")
In Book class:
class Book:
...
def get_title_or_nothing(self):
if self.type == WEIRD_TYPE:
return ""
return self.title
UPDATED:
class BookAdmin(admin.ModelAdmin):
list_display = ("pk", "get_title_or_nothing")
def get_form(self, request, obj=None, **kwargs):
if obj.type == "1":
self.exclude = ("title", )
form = super(BookAdmin, self).get_form(request, obj, **kwargs)
return form
190👍
For Django > 1.8 one can directly set the fields to be excluded in admin:
class PostCodesAdmin(admin.ModelAdmin):
exclude = ('pcname',)
Hidden fields are directly defined in Django’s ORM by setting the Field attribute: editable = False
e.g.
class PostCodes(models.Model):
gisid = models.IntegerField(primary_key=True)
pcname = models.CharField(max_length=32, db_index=True, editable=False)
...
However, setting or changing the model’s fields directly may not always be possible or advantegous. In principle the following admin.py
setup could work, but won’t since exclude is an InlineModelAdmin option.
class PostCodesAdmin(admin.ModelAdmin):
exclude = ('pcname',)
....
A solution working at least in Django 1.4 (and likely later version numbers) is:
class PostCodesAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(PostCodesAdmin, self).get_form(request, obj, **kwargs)
del form.base_fields['enable_comments']
return form
For the admin list-view of the items, it suffices to simply leave out fields not required:
e.g.
class PostCodesAdmin(admin.ModelAdmin):
list_display = ('id', 'gisid', 'title', )
- [Django]-Django Admin – Disable the 'Add' action for a specific model
- [Django]-How do I run tests against a Django data migration?
- [Django]-"Post Image data using POSTMAN"
8👍
class BookAdmin(admin.ModelAdmin):
exclude = ("fieldname",) # hide fields which you want
- [Django]-How do you filter a nested serializer in Django Rest Framework?
- [Django]-Set Django's FileField to an existing file
- [Django]-Django override save for model only in some cases?
4👍
I tried to override get_form()
function but some mix up errors occur when I switch in different records. I found there is a get_exclude()
function we can override.
Use:
class BookAdmin(admin.ModelAdmin):
def get_exclude(self, request, obj=None):
if obj and obj.type == "1":
# When you create new data the obj is None
return ("title", )
return super().get_exclude(request, obj)
- [Django]-Overriding AppConfig.ready()
- [Django]-Django TemplateDoesNotExist?
- [Django]-Django: Get list of model fields?
0👍
Apropos @Lorenz @mrts answer
with Django 2.1 I found that exclude does not work if the field is already specified via fields = .
In that case you may use
self.fields.remove('title')
fields will have to be defined as a list [] for this to work
- [Django]-">", "<", ">=" and "<=" don't work with "filter()" in Django
- [Django]-*_set attributes on Django Models
- [Django]-Django – iterate number in for loop of a template
0👍
If you want to maintain the value in the form (for example set a value, i.e. user, based on the request) and hide the field, you can change the widget to forms.HiddenInput()
:
from django import forms
...
def get_form(self, request, obj=None, **kwargs):
"""Set defaults based on request user"""
# update user field with logged user as default
form = super().get_form(request, obj, **kwargs)
form.base_fields["user"].initial = request.user.id
form.base_fields["user"].widget = forms.HiddenInput()
return form
- [Django]-Django Rest Framework custom response message
- [Django]-How to save pillow image object to Django ImageField?
- [Django]-Get user profile in django
0👍
Here is a working example
class BookAdmin(admin.ModelAdmin):
def get_fieldsets(self, request, obj):
if obj is None:
return [
(
None,
{'fields': ('type', 'description',)}
)
]
elif request.user.is_superuser:
return [
(
None,
{'fields': ('type', 'status', 'author', 'store', 'description',)}
)
]
else:
return [
(
None,
{'fields': ('type', 'date', 'author', 'store',)}
)
]
- [Django]-How to get the current URL within a Django template?
- [Django]-Django Model Field Default Based Off Another Field in Same Model
- [Django]-Best practice for Django project working directory structure