Can I change readonly_fields
in my TranslationAdmin
class dependent on the value of a certain field in the Translation
being viewed? If so, how would I do that?
The only thing I've come up with is to make a widget that looks at the Translation
and decides whether to be a readonly widget or not, but that seems like overkill for this.
I think you have misunderstood what fieldsets are. It's just a way for you to be able to group the fields on a Model Admin Page.
One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.
The attribute prepopulated_fields tells the admin application to automatically fill the field slug - in this case with the text entered into the name field.
You can inherit the function get_readonly_fields() in your admin and set readonly fields according your model's certain field value
class TranslationAdmin(admin.ModelAdmin):
...
def get_readonly_fields(self, request, obj=None):
if obj.certainfield == something:
return ('field1', 'field2')
else:
return super(TranslationAdmin, self).get_readonly_fields(request, obj)
I hope it will help you.
Here is an example of:
Inheriting from that ModelAdmin (ShapefileSetAdmin) and adding an additional value to readonly_fields
class GISDataFileAdmin(admin.ModelAdmin):
# Note(!): this is a list, NOT a tuple
readonly_fields = ['modified', 'created', 'md5',]
class ShapefileSetAdmin(GISDataFileAdmin):
def get_readonly_fields(self, request, obj=None):
# inherits readonly_fields from GISDataFileAdmin and adds another
# retrieve current readonly fields
ro_fields = super(ShapefileSetAdmin, self).get_readonly_fields(request, obj)
# check if new field already exists, if not, add it
#
# Note: removing the 'if not' check will add the new read-only field
# each time you go to the 'add' page in the admin
# e.g., you can end up with:
# ['modified', 'created', 'md5', 'shapefile_load_path', 'shapefile_load_path, 'shapefile_load_path', etc.]
#
if not 'shapefile_load_path' in ro_fields:
ro_fields.append('shapefile_load_path')
return ro_fields
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With