Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name model's foreignkey field in Django?

In Django, I can name each field of model like this(In Korean):

description = models.CharField("설명", max_length=100, blank=True)
image = ImageField("이미지", upload_to=upload_location)

but in case of ForeignKey, it doesn't work.

album = models.ForeignKey("앨범", Album)

Why does it not work only on ForeignKey?

like image 326
user3595632 Avatar asked Sep 16 '25 02:09

user3595632


1 Answers

Because the first parameter of a ForeignKey is the model it is linking to, this can be a string such as 'self' which is why it allows strings.

If you want to specify a verbose_name then you should use the keyword arg

album = models.ForeignKey(Album, verbose_name="앨범")

For more on verbose_names, see the docs for Verbose field names

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

like image 156
Sayse Avatar answered Sep 17 '25 19:09

Sayse