简体   繁体   中英

Django REST Framework Foreign Key - NOT NULL constraint failed

I have the following setup in Django REST Framework:

models.py:

class Device(models.Model):
    name = models.CharField()
    latitude = models.FloatField()
    longitude = models.FloatField()
    altitude = models.FloatField()

class Event(models.Model):
    id_device = models.ForeignKey(Device, related_name='events')
    name = models.CharField()
    date = models.CharField()
    time = models.CharField()

class Values(models.Model):
    id_device = models.ForeignKey(Device)
    id_event = models.ForeignKey(Event, related_name='values')
    x = models.FloatField()
    y = models.FloatField()
    z = models.FloatField()
    time = models.IntegerField()

serializers.py:

class DeviceSerializer(serializers.ModelSerializer):    

    events = EventSerializer(many=True)

    class Meta:
        model = Device
        fields = ('url', 'id', 'name', 'latitude', 'longitude', 'altitude', 'events')

    def create(self, validated_data):
        events_data = validated_data.pop('events')
        device = Device.objects.create(**validated_data)
        for event in events_data:
            Events.objects.create(device=device, **event)
        return device

class EventSerializer(serializers.ModelSerializer):

    values = ValuesSerializer(many=True)

    class Meta:
        model = Event
        fields = ('url', 'id', 'name', 'date', 'time', 'values')

    def create(self, validated_data):
        values_data = validated_data.pop('values')
        event = Event.objects.create(**validated_data)
        for value in values_data:
            Values.objects.create(event=event, **value)
        return event

class ValuesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Values
        fields = ('x', 'y', 'z', 'time')

When I try to post an event with some values assigned using a JSON file like this one:

{
    "name": "name_example", 
    "date": "date_example", 
    "time": "time_example", 
    "values": [
      {
        "x": 1, 
        "y": 2, 
        "z": 3, 
        "time": 1
      }, 
      {
        "x": 10, 
        "y": 20, 
        "z": 30, 
        "time": 2
      }, 
      {
        "x": 100, 
        "y": 200, 
        "z": 300, 
        "time": 4
      }
   ]
}

I get the error IntegrityError: NOT NULL constraint failed: drf_event.id_device_id

I'm new to this framework, so what can I do in order to post new events with values assigned to an existing device ?

You haven't a key pointing to device in your EventSerializer. You miss the id_device.

class EventSerializer(serializers.ModelSerializer):

    values = ValuesSerializer(many=True)

    class Meta:
        model = Event
        fields = ('id_device', 'url', 'id', 'name', 'date', 'time', 'values')

And you need to add the key in your json:

{
    "id_device": 1,
    "name": "name_example", 
    "date": "date_example", 
    "time": "time_example", 
    "values": [
      {
        "x": 1, 
        "y": 2, 
        "z": 3, 
        "time": 1
      }, 
      {
        "x": 10, 
        "y": 20, 
        "z": 30, 
        "time": 2
      }, 
      {
        "x": 100, 
        "y": 200, 
        "z": 300, 
        "time": 4
      }
   ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM