简体   繁体   中英

How to show name in foreign key?

在此处输入图像描述

In Models:

 class ShopCategory(models.Model): category = models.CharField(max_length=100) class Meta: db_table = "tbl_ShopCategory" class Shop(models.Model): id_shop = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) name = models.CharField(max_length=200) slug = models.SlugField(max_length=250, blank=True, null=True) cover_img = models.CharField(max_length=250, blank=True) category = models.ForeignKey(ShopCategory, on_delete=models.CASCADE) avgscore = models.FloatField(default=0)

I want to show the Category name? ...........................................................

Add an str method to your ShopCategory model:

class ShopCategory(models.Model):
    category = models.CharField(max_length=100)

    def __str__(self):
        return self.category

    class Meta:
        db_table = "tbl_ShopCategory"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM