views:

260

answers:

2

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?

A: 

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?

Daniel Roseman
I commented above on my reasoning, and it's starting to look like a bad idea.Really though, how can I edit primary keys in django's admin. Let's say I'm masochistic.
emcee
+1  A: 

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

Haes
You are certainly right. I opted for an extra field that's optional and prefacing every regular check with a call for that key. I suppose Django filters either any AutoKey fields or just filters out keys named id.Kind of disappointing that it won't let me force bad practices upon it.
emcee
Yes it seems that it filters AutoField's which makes sense. But if you specify it as an IntegerField you'll be able to edit it. See my updated answer.
Haes