I created a models.py looking this way:

class Customer(models.Model):
    zip = models.IntegerField("Postleitzahl") 
    place = models.CharField("Ort", max_length=32)
    customer_number = models.CharField("Kundennummer", max_length=32) 
    bank = models.ForeignKey("Bank", Bank)


    def __unicode__(self):
        return unicode(self.first_name) + ' ' + unicode(self.last_name)

    def get_absolute_url(self):
        return "/customers/detail/%i" % self.id

Now i would like to "rename" my attributes in the model. For example, i want to display the word "Postleitzahl" instead of zip. This works really good, but when i tried to "rename" the ForeignKey bank, my manage.py isn't able to run the server.

Does anyone know, how to rename a ForeignKey? Thank you very much for your help!

有帮助吗?

解决方案

Add verbose_name argument to it:

bank = models.ForeignKey(Bank, verbose_name="Bank")

From docs:

Each field type, except for ForeignKey, ManyToManyField and OneToOneField, takes an optional first positional argument – a verbose name. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces.

ForeignKey, ManyToManyField and OneToOneField require the first argument to be a model class, so use the verbose_name keyword argument

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top