38
I also wanted to simply show the ‘id’ (primary key) within the Django admin, but not necessarily edit it. I just added it to the readonly_fields
list, and it showed up fine. IE:
class StudentEnrollmentInline(admin.TabularInline):
model = Enrollment
readonly_fields=('id',)
whereas if I tried to add it to the ‘fields’ list, Django got upset with me, saying that field didn’t exist…
15
If you explicitly specify the primary key field in your models (with primary_key=True
), you should be able to edit it in the admin.
For Django models created via ./manage.py syncdb
the following primary key field is added automatically:
id = models.AutoField(primary_key=True)
if you change (or add) that to your model explicitly as an IntegerField primary key, you’ll be able to edit it directly using the admin:
id = models.IntegerField(primary_key=True)
But as others pointed out, this is a potential minefield…
- [Django]-Django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. (django 2.0.1)(Python 3.6)
- [Django]-Django – Get only date from datetime.strptime
- [Django]-Get list item dynamically in django templates
4
To show the primary key, which by default will have a column name of “id” in the database – use “pk”
def __str__(self):
return '{} - {} ({})'.format(self.pk, self.name, self.pcode)
- [Django]-Django – form has no errors but form.is_valid() doesn't validate
- [Django]-Django form fails validation on a unique field
- [Django]-Django admin – how to make "inlines" collapsible?
1
It doesn’t make sense to have an optional primary key. Either the PK is an autoincrement, in which case there’s no need to edit it, or it’s manually specified, in which case it is always required.
Why do you need this?
- [Django]-Get javascript variable's value in Django url template tag
- [Django]-Pipfile.lock out of date
- [Django]-In django do models have a default timestamp field?
1
In django documentation, there is a short sentence about that, which is not clear:
If neither fields nor fieldsets options are present, Django will default to displaying each field that isn’t an AutoField and has editable=True, in a single fieldset, in the same order as the fields are defined in the model.
Reason is, django do not allow you to edit an AutoField
by any means (and that is the right thing since it is an auto increment value and should not be edited). @mnelson4’s answer is a good approach to display it.
- [Django]-Create empty queryset by default in django form fields
- [Django]-ValueError: Dependency on app with no migrations: customuser
- [Django]-Django TemplateSyntaxError – 'staticfiles' is not a registered tag library
1
The answer with the highest votes didn’t work for me. I needed a getter.
class StudentEnrollmentInline(admin.TabularInline):
model = Enrollment
readonly_fields=('student_enrollment_id',)
def student_enrollment_id(self, obj):
return obj.id
Using django 1.11
- [Django]-How to test custom template tags in Django?
- [Django]-AttributeError while using Django Rest Framework with serializers
- [Django]-Bulk create model objects in django