80π
β
After a while of trying to find the name I figured out thanks to this answer, so I checked the names at self.opts.local_fields
and found the name of the middle table and added it to readonly_fields
, setting can_delete
to False.
class SummaryInline(admin.TabularInline):
model = ParserError.summaries.through
readonly_fields = ('myclasssummary',)
can_delete = False
pretty simple but took me a while so I figured out it was a good idea to add it here.
π€Hassek
49π
Additionally, if you do not want the ability to add/delete the rows, you can add these definitions.
def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
π€Keval Prabhu
- [Django]-Django. You don't have permission to edit anything
- [Django]-Prepopulate Django (non-Model) Form
- [Django]-Error: Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>
39π
You can make the entire inline readonly by adding:
class UnitsInline(admin.TabularInline):
def has_change_permission(self, request, obj=None):
return False
This will prevent anyone from editing the entry from the admin.
Another example that prevents, adding, deletion and displays all the inline fields as readonly:
class ReadOnlyInline(admin.TabularInline):
def has_change_permission(self, request, obj=None):
return False
def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
def get_readonly_fields(self, request, obj=None):
return list(super().get_fields(request, obj))
π€tamarabyte
- [Django]-Django: is there a way to count SQL queries from an unit test?
- [Django]-What's the difference between ContentType and MimeType?
- [Django]-Django query datetime for objects older than 5 hours
5π
Thanks Keval Prabhu
class UnitsInline(admin.TabularInline):
model = Units
extra = 0
verbose_name = 'Units'
verbose_name_plural = 'Units of company'
def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
π€Muhammad Hafid
- [Django]-Django admin make a field read-only when modifying obj but required when adding new obj
- [Django]-Profiling Django
- [Django]-How do Django models work?
Source:stackexchange.com