Raphael
Raphael

Reputation: 576

Django Field Rename

Is that possible to rename field ?

Example:

models.py

class Person(models.Model):
    nameP = models.CharField(max_length=40)
    ageP  = models.CharField(max_length=40)
    def __unicode__(self):
        return self.nameP

And now I want to print in my addPerson.html :

Name : Person 1 Age : 18

Thank you !

Upvotes: 1

Views: 273

Answers (3)

Ryabchenko Alexander
Ryabchenko Alexander

Reputation: 12380

You can rename field in model:

  1. Edit the field name in the model (but remember the old field name: you need it for step 3!)
  2. Create an empty migration

$ python manage.py makemigrations --empty myApp

  1. Edit the empty migration (it's in the migrations folder in your app folder, and will be the most recent migration) by adding to the operations list

migrations.RenameField('MyModel', 'old_field_name', 'new_field_name')

  1. Apply the migration

$ python manage.py migrate myApp

Upvotes: 0

bnjmn
bnjmn

Reputation: 4584

You could use verbose_name within the field parameter like this:

nameP = models.CharField(max_length=40, verbose_name='Name')
ageP  = models.CharField(max_length=40, verbose_name='Age')
...

You can also do this on Model classes as well with the Meta class. Say your Person class was actually named PPPerson, like this:

class PPPerson(models.Model):
    nameP = models.CharField(max_length=40, verbose_name='Name')
    ageP  = models.CharField(max_length=40, verbose_name='Age')
    class Meta:
        verbose_name = 'Person'
        verbose_name_plural = 'People'

Upvotes: 2

jproffitt
jproffitt

Reputation: 6355

Use the `verbose_name' parameter:

class Person(models.Model):
    nameP = models.CharField(max_length=40, verbose_name='Name')
    ageP  = models.CharField(max_length=40, verbose_name='Age')

    def __unicode__(self):
        return self.nameP

Upvotes: 0

Related Questions