tags:

views:

555

answers:

2

I am using 0.97-pre-SVN-unknown release of Django.

I have a model for which I have not given any primary_key. Django, consequently, automatically provides an AutoField that is called "id". Everything's fine with that. But now, I have to change the "verbose_name" of that AutoField to something other than "id". I cannot override the "id" field the usual way, because that would require dropping/resetting the entire model and its data (which is strictly not an option). I cannot find another way around it. Does what I want even possible to achieve? If you may suggest any alternatives that would get me away with what I want without having to drop the model/table, I'd be happy.

Many Thanks.

+2  A: 

Look into the command-line options for manage.py; there's a command to dump all of the model data to JSON, and another command to load it back in from JSON. You can export all of your model data, add your new field to the model, then import your data back in. Just make sure that you set the db_column option to 'id' so you don't break your existing data.

Edit: Specifically, you want the commands dumpdata and loaddata.

Justin Voss
+1: Database surgery is a necessary evil. Since there's no way around situations where dump, drop and load are necessary, it's good to have a recipe for doing this.
S.Lott
Thanks for the suggestion, Justin.
ayaz
+2  A: 

Hmm... and what about explicitly write id field in the model definition? Like this for example:

class Entry(models.Model):
   id = models.AutoField(verbose_name="custom name")
   # and other fields...

It doesn't require any underlying database changes.

Alex Koshelev
Interesting. I would've thought that would require me to drop/reset the model table, but now that I've tried it, it does not. Works perfectly. Thanks.
ayaz