str function in a django model returns a string that is exactly rendered as the display name of instances for that model.
def str(self): is a python method which is called when we use print/str to convert object into a string. It is predefined , however can be customised.
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.
It is easy, just overload has_add_permission
method in your Admin
class like so:
class MyAdmin(admin.ModelAdmin):
def has_add_permission(self, request, obj=None):
return False
I think this will help you.. below code must be in admin.py file
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = ('name', )
list_filter = ('name', )
search_fields = ('name', )
list_per_page = 20
# This will help you to disbale add functionality
def has_add_permission(self, request):
return False
# This will help you to disable delete functionaliyt
def has_delete_permission(self, request, obj=None):
return False
In additon to the above as posted by
# This will help you to disable change functionality
def has_change_permission(self, request, obj=None):
return False
By default syncdb creates 3 security permissions for each model:
If your logged in as Admin, you get EVERYTHING no matter what.
But if you create a new user group called "General Access" (for example) then you can assign ONLY the CHANGE and DELETE permissions for all of your models.
Then any logged in user that is a member of that group will not have "Create" permission, nothing related to it will show on the screen.
# In admin
# make the related field can't be added
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
form.base_fields['service'].widget.can_add_related = False
return form
# In inline formset e.g. admin.TabularInline
# disable all
def get_formset(self, request, obj=None, **kwargs):
formset = super().get_formset(request, obj, **kwargs)
service = formset.form.base_fields['service']
service.widget.can_add_related = service.widget.can_change_related = service.widget.can_delete_related = False
return formset
in service = formset.form.base_fields['service']
base_fields
is the fields defined in model
if defined in the form use:
product = formset.form.declared_fields['product']
see also
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