0

我已经链接了模型-> 类别-品牌-类型。例如 :

  • 类别 : 轻型车
  • 品牌:大众
  • 类型:帕萨特

我有一个帖子模型。问题:如何在表单中获取链接对象?代码:


class Category(models.Model):
    title = models.CharField(max_length=32)
    slug = models.SlugField(max_length=32)

    def __str__(self):
        return "%s" % self.title

class Brand(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    title = models.CharField(max_length=32)

    def __str__(self):
        return "%s" % self.title

class Type(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    brand = ChainedForeignKey(
        'Brand',
        chained_field="category",
        chained_model_field="category",
        show_all=False,
        auto_choose=True
    )
    title = models.CharField('Модель автомобіля', max_length=32)
    slug = models.SlugField(max_length=32)

    def __str__(self):
        return "%s" % self.title

岗位型号:

class Post(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
    type = models.ForeignKey(Type, on_delete=models.CASCADE)
......

    def __str__(self):
        return "{} {}".format(self.brand, self.type)

我需要创建一个带有链接对象的表单。我怎样才能得到它?或者如何编辑我的帖子模型?

4

0 回答 0