Django Rest + Mongoengine - Allow model fields to be optional

243 Views Asked by At

I've setup a REST api with django rest framework, using mongoengine for models. However, By default all the fields of model are mandatory but I want to make some of the fields as optional.

my model.py is :

class ProjectFormula(EmbeddedDocument):
    name = fields.StringField()
    expression = fields.StringField()

class ProjectMeta(Document):
    project_id  = fields.IntField()
    sheet_mapping = fields.DictField()
    classificication_map = fields.DictField()
    concept_map = fields.DictField()
    formulas = fields.ListField(fields.EmbeddedDocumentField(ProjectFormula))

serializers.py

class ProjectMetaSerializer(mongoserializer.DocumentSerializer):
    class Meta:
        model  = ProjectMeta
        fields = '__all__' 

response from Post api:

{
    "sheet_mapping": [
        "This field is required."
    ],
    "classificication_map": [
        "This field is required."
    ],
    "concept_map": [
        "This field is required."
    ]
}

I want to make this fields optional, only project_id and sheet_mapping is mandatory.

I tried classificication_map = fields.DictField(required=False) in model.py [From some other research] but didn't work.

Any help will be appreciated.

0

There are 0 best solutions below