django模型字段声明中的条件

时间:2017-10-28 18:50:29

标签: django django-models

我有一个名为Error: Cannot determine the module for class SupplierChequeModalPage in C:/wamp/www/pos/src/pages/supplier-cheque-modal/supplier-cheque -modal.ts! Add SupplierChequeModalPage to the NgModule to fix it. Cannot determine the module for class LetterAvatarDirective in C:/wamp/www/pos/src/directives/letter-avatar/letter-avatar.ts! Add Lette rAvatarDirective to the NgModule to fix it. Cannot determine the module for class ClientChequeModalPage in C:/wamp/www/pos/src/pages/client-cheque-modal/client-cheque-modal.ts! Ad d ClientChequeModalPage to the NgModule to fix it. Cannot determine the module for class CustomerModalPage in C:/wamp/www/pos/src/pages/customer-modal/customer-modal.ts! Add CustomerModa lPage to the NgModule to fix it. Cannot determine the module for class LowStockFilterPipe in C:/wamp/www/pos/src/pipes/low-stock-filter/low-stock-filter.ts! Add LowStoc kFilterPipe to the NgModule to fix it. Cannot determine the module for class DateFilterPipe in C:/wamp/www/pos/src/pipes/date-filter/date-filter.ts! Add DateFilterPipe to the NgModule to fix it. at Error (native) at syntaxError (C:\wamp\www\pos\node_modules\@angular\compiler\bundles\compiler.umd.js: 1550:34) at analyzeAndValidateNgModules (C:\wamp\www\pos\node_modules\@angular\compiler\bundles\compiler.umd.js: 22948:15) at AotCompiler.compileAll (C:\wamp\www\pos\node_modules\@angular\compiler\bundles\compiler.umd.js: 22721:18) at CodeGenerator.codegen (C:\wamp\www\pos\node_modules\@angular\compiler-cli\src\codegen.js:30:14) at Function.NgTools_InternalApi_NG_2.codeGen (C:\wamp\www\pos\node_modules\@angular\compiler-cli\src\ngtools_api.js: 61:30)at Object.doCodegen (C:\wamp\www\pos\node_modules\@ionic\app- scripts\dist\aot\codegen.js:6:51)at C:\wamp\www\pos\node_modules\@ionic\app- scripts\dist\aot\aot-compiler.js:42:30 的模型,我用它来存储绘画和照片。 在此模型中,我有一个Image字段,用于存储创建者的名称。

由于与此问题无关的原因,如果图片实例是照片,则Artist 可以为空,但如果{{> ,则 1}}实例是一幅画。因此,我想要这样的东西:

Artist

这样的条件可能吗?

1 个答案:

答案 0 :(得分:1)

您可以做的是使用blank=True声明字段并覆盖表单的de clean方法,以确保isPaintingTrue时指定艺术家。< / p>

<强> Models.py

class Image(models.Model)

    isPainting = models.BooleanField(default=0) # set too 1 for paintings
    artist = models.CharField(max_length=127, blank=True)

<强> Forms.py

 def clean(self):
    cleaned_data = super(ImageForm, self).clean()
    isPainting = cleaned_data.get("isPainting")
    artist = cleaned_data.get("artist")

    if isPainting and not artist :
        raise ValidationError('An artist must be specified since it is a painting')

    return cleaned_data

如果isPainting为True

,则禁止添加艺术家