bgarcial
bgarcial

Reputation: 3213

Django Rest Framework - Serializing Many To Many fields attributes to POST operation

I have the following model

class Metrics(models.Model):
    name = models.CharField(
        max_length=255,
        blank=True,
        verbose_name='Nombre'
    )
    value = models.DecimalField(
        max_digits = 5,
        decimal_places = 3,
        verbose_name = 'Valor',
        null = True,
        blank = True
    )

And in another model I have the ManyToManyField relationship to the Metrics model

class PatientMonitoring(models.Model):
    metrics = models.ManyToManyField(Metrics, verbose_name='Métricas', 

)

My serializers.py file look of this way:

class MetricsSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model= Metrics
        fields = ('name','value',)

class PatientMonitoringSerializer(serializers.HyperlinkedModelSerializer):
    metrics = MetricsSerializer(many=True)

    class Meta:
        model = PatientMonitoring
        fields = ('url', 'id', 'metrics')
        #depth=2

    def create(self,validated_data):
        metrics_data = validated_data.pop('metrics')
        metric = Metrics.object.create(**validated_data)
        for metric_data in metrics_data:
            Metrics.objects.create(metric=metric,**metrics_data)
        metric.save()
        return metric

The result are that my models are being serialized, but they dont `have POST permissions and the form view, is not being update or create

enter image description here

I am defining the create method, which is described here for work with models serialized and write/post operations, but I don't understand how the process is doing all this functionality. How to can I work with my model and attribute with POST methods in a case like this?

*UPDATE

I've performed a POST operation via curl such as @RossRogers and @Nirri suggest me and the behavior is the following:

curl -X POST http://localhost:8000/api/patientmonitoring/ -d "metrics=Grados" 

And I get this message in relation to my create method

File "/home/bgarcial/.virtualenvs/nrhb_dev/lib/python3.5/site-packages/rest_framework/serializers.py", line 191, in save
    self.instance = self.create(validated_data)
  File "/home/bgarcial/workspace/neurorehabilitation-project/patient_monitoring/serializers.py", line 37, in create
    metric = Metrics.object.create(**validated_data)
AttributeError: type object 'Metrics' has no attribute 'object'
[28/Jun/2016 17:19:16] "POST /api/patientmonitoring/ HTTP/1.1" 500 104669

Upvotes: 1

Views: 2430

Answers (1)

xtrinch
xtrinch

Reputation: 2291

Well, as it says, lists are not supported in HTML input.

As you can see here (http://www.django-rest-framework.org/topics/3.2-announcement/), it was planned for 3.3 release but there's no mention of it in the release notes.

It doesn't mean you don't have permissions to do POST requests. Try making a POST request with raw JSON (as suggested in a comment on your post) with DHC, Postman or even curl.

Upvotes: 1

Related Questions