문제

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