I'm using drf_mongoengine for the first time and I'm having problems setting up the models. I want the documents to be initialized like this:
{
"name" : "new_name",
"metadata": {
"total_efficiency": 0.0,
"eff_vs_layer_thickness":{
"x":[],
"y":[]
}
}
}
The documents are created without the "metadata" field. What Am I missing?
Models: class Detector(Document): name = fields.StringField(null=True) metadata = fields.EmbeddedDocumentField(Metadata, null=False)
class Metadata(EmbeddedDocument):
eff_vs_layer = fields.EmbeddedDocumentField(Plot)
total_efficiency = fields.DecimalField(null=True, default=0)
class Plot(EmbeddedDocument):
x = fields.ListField(fields.FloatField(null=True), default=[])
y = fields.ListField(fields.FloatField(null=True), default=[])
Serializer:
class DetectorSerializer(mongoserializers.DocumentSerializer):
class Meta:
model = Detector
fields = '__all__'
class MetadataSerializer(mongoserializers.EmbeddedDocumentSerializer):
class Meta:
model = Metadata
fields = '__all__'
View:
class DetectorViewSet(viewsets.ModelViewSet, mixins.UpdateModelMixin, mixins.DestroyModelMixin):
'''
Contains information about inputs/outputs of a single program
that may be used in Universe workflows.
'''
lookup_field = 'id'
serializer_class = DetectorSerializer
@alvcarmona, welcome to DRF-ME. You're generally doing everything right.
Just a couple of things: you don't need MetadataSerializer
, as it will be created automatically inside DetectorSerializer
.
You also shouldn't mix mixins.UpdateModelMixin
and mixins.DestroyModelMixin
into a full viewset (viewsets.ModelViewSet
), instead, mix them into rest_framework_mongoengine.generics.GenericApiView
(like here: https://github.com/umutbozkurt/django-rest-framework-mongoengine/blob/master/rest_framework_mongoengine/generics.py#L16).
Other than that, I think, you're doing everything right. If you can show me your project on github, I could help more.
UPDATE: to mix mixins into generic view, do it as we do here e.g.:
class PostPutViewSet(mixins.CreateModelMixin,
mixins.UpdateModelMixin,
GenericViewSet):
""" Adaptation of DRF ModelViewSet """
pass