44👍
You can add it to readonly_fields
in the modeladmin class.
class BookAdmin(admin.ModelAdmin):
readonly_fields = ('id',)
admin.site.register(Book, BookAdmin)
3👍
EDIT
I removed my answer since it was the same as @DanielRoseman, but this is just for clarification. This is from the documentation:
If False, the field will not be displayed in the admin or any other
ModelForm. They are also skipped during model validation. Default is
True.
Therefore, using readonly_fields is the way to go.
- [Django]-Using south to refactor a Django model with inheritance
- [Django]-What does 'many = True' do in Django Rest FrameWork?
- [Django]-Django Aggregation: Summation of Multiplication of two fields
1👍
To display the primary key (PK) of models in Django admin, you can customize the admin interface by creating a ModelAdmin class for your model and override the list_display attribute. Here’s an example:
from django.contrib import admin
from .models import YourModel
class YourModelAdmin(admin.ModelAdmin):
list_display = ('pk', '__str__',) # Add 'pk' to the list_display
admin.site.register(YourModel, YourModelAdmin)
In the example above, we created a YourModelAdmin class that inherits from admin.ModelAdmin. We added ‘pk’ to the list_display attribute to include the primary key in the list view of the model in the Django admin interface. You can also include other fields you want to display in the list view, separated by commas.
- [Django]-Validators = [MinValueValidator] does not work in Django
- [Django]-Make django model field read only or disable in admin while saving the object first time
- [Django]-Validation of dependant inlines in django admin