Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit/show Primary Key in Django Admin

It appears Django hides fields that are flagged Primary Key from being displayed/edited in the Django admin interface.

Let's say I'd like to input data in which I may or may not want to specify a primary key. How would I go about displaying primary keys in the admin interface, and how could I make specifying it optional?

like image 539
emcee Avatar asked Mar 28 '10 03:03

emcee


People also ask

How do I change the admin text in Django?

To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.

How do I find my Django ID?

One way to show the ID field is to make it editable by removing editable=False from the model definition. However, I would like to keep it read-only. Possible duplicate of Django admin: How to display a field that is marked as editable=False' in the model?


6 Answers

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...

like image 182
thespacecamel Avatar answered Oct 18 '22 01:10

thespacecamel


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...

like image 44
Haes Avatar answered Oct 18 '22 02:10

Haes


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)
like image 40
mgrollins Avatar answered Oct 18 '22 01:10

mgrollins


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?

like image 32
Daniel Roseman Avatar answered Oct 18 '22 03:10

Daniel Roseman


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.

like image 23
FallenAngel Avatar answered Oct 18 '22 03:10

FallenAngel


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

like image 36
Frank Henard Avatar answered Oct 18 '22 02:10

Frank Henard