【问题标题】:ModelForm problem模型形式问题
【发布时间】:2011-02-19 10:50:36
【问题描述】:

我已经声明了我的模型类,如 this link....我现在想自定义我为 Vehicle 对象添加/编辑 ModelForm 的渲染方式,我想要年份、品牌、型号和制造商字段将单独呈现,而不是引用 Vehicle 类中的一个 common_vehicle 字段。如何做到这一点?

【问题讨论】:

    标签: django django-forms


    【解决方案1】:

    为什么不让 Vehicle 继承 CommonVehicle? (当然,这取决于你为什么在那里有那个 FK——你可能真的需要它,但我猜不是)

    代替:

    class Vehicle(models.Model):
        ...
        common_vehicle = models.ForeignKey(CommonVehicle)
    

    用途:

    class Vehicle(CommonVehicle):
        ...all your other Vehicle fields here, but not the FK to CommonVehicle
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-21
      • 2013-11-29
      • 2019-03-03
      • 2019-12-18
      • 2019-09-07
      • 2015-02-27
      相关资源
      最近更新 更多