0

I have the following models

class Peri(models.Model):
    date = models.DateField()
    customer = models.ForeignKey(Customer)

class PeriTask(models.Model):
    #fields
    peri = models.ForeignKey(Peri)

My serializers are the following

class PeriSerializer(serializers.HyperlinkedModelSerializer):
    customer = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = Peri
        fields = ('id', 'date', 'url', 'peritasks', 'customer')


class PeriTaskSerialiazer(serializers.HyperlinkedModelSerializer):
    tooth = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = PeriTask
        fields = ('id', 'task_type', 'implant', 'furcation', 'bleeding1', 'bleeding2', 'bleeding3', 'plaque1', 'plaque2',
              'gingival_margin1', 'gingival_margin2', 'gingival_margin3', 'probing_depth1', 'probing_depth2',
              'probing_depth3', 'tooth', 'url', )

and my viewsets are

class PeriodontogrammaViewSet(ModelViewSet):
    serializer_class = PeriSerializer
    queryset = Peri.objects.all()


class PeriTaskViewSet(ModelViewSet):
    serializer_class = PeriTaskSerialiazer
    queryset = PeriTask.objects.all()

But when I try to create a new peri using the api it gives me the following integrity error

NOT NULL constraint failed: peri_peri.customer_id

My json data that beeing posted are

{"date": "2014-12-17",
 "customer": 27
}

I haven't created a serializer for customer since I am not interested in having api for my other models.

4
  • It's a standard view set...will include it in intial post Commented Dec 17, 2014 at 14:39
  • Maybe you should post "customer_id": 27 ? Commented Dec 17, 2014 at 14:39
  • No I get the same result with "customer_id" Commented Dec 17, 2014 at 14:43
  • yes yes sorry editing right now Commented Dec 17, 2014 at 14:48

1 Answer 1

1

In your serializer, you've set the customer key to read_only:

customer = serializers.PrimaryKeyRelatedField(read_only=True)

Try setting it to False or just simply removing this whole line (which seems superfluous to me)

Sign up to request clarification or add additional context in comments.

3 Comments

True...removing read_only did it. But is is it possible to make it read_only when on detail-view of peri's. I don't wan't this to be able to be edited with put request. Or is it all or nothing?
You are free to make as many serializer as you need. Why not having one read_only and not the other?
What do you mean? Could you be more specific if you please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.