0

I am attempting to POST a nested relationship with Django REST Framework (2.3.9), but I am getting this error:

ValueError: Cannot add "": instance is on database "default", value is on database "None"

It seems that Django (1.6) is not recognizing that my HeartbeatSerializer knows anything about ItemSerializer

{
    "username":"testing",
    "heartbeat": {
        "items": [
            {
                "item_date": "2013-11-24T05:08:12Z", 
                "item_title": "disagreeing post", 
                "item_type": "post", 
                "item_karma": 25
            }
        ]
    }
}

Here's the serializers.py:

class ItemSerializer(serializers.ModelSerializer):
    '''
        Item Serializer

        Items are User-generated Content which are captured 
        from various APIs made available from the Web or 
        Internet via REST consumed endpoints or Websites 
        which have been scraped.
    '''

    class Meta:
        model = Item
        # fields = ('id', 'item_title', 'item_karma',
        #           'item_type', 'item_date', )


class DepthPatchSerializer(serializers.ModelSerializer):
    def __init__(self, model=None, **kwargs):
        if model is not None:
            self.Meta.model = model
        super(DepthPatchSerializer, self).__init__(**kwargs);
    def get_nested_field(self, model_field):
        return DepthPatchSerializer(model=model_field.rel.to)


class HeartbeatSerializer(DepthPatchSerializer):
    '''
        Heartbeat Serializer

        Items are User-generated Content (USG) containers which are used
        to consolidate normalized User-generated Interactions (USI).
    '''

    # items_queryset = Item.objects.all()
    # items = ItemSerializer(items_queryset, many=True)

    class Meta:
        model = Heartbeat
        # fields = ('id', 'items', )
        depth = 1


class HackerAddSerializer(serializers.ModelSerializer):
    '''
        User Serializer

        Each User is stored in the system itself for analysis and signaling.

        We store users such that subsets of user behavior through USG can be
        generated and analyzed.
    '''

    heartbeat = HeartbeatSerializer()

    def _save_heartbeat_data(self):
        heartbeat_data = self.init_data.get('heartbeat', None)
        if heartbeat_data:
            hs = HeartbeatSerializer(instance=self.object.heartbeat,
                                    data=heartbeat_data)
            import pdb; pdb.set_trace()  # XXX BREAKPOINT
            if hs.is_valid():
                self.object.heartbeat = hs.object
                hs.save()
            else:
                raise Exception(hs.errors)

    def save(self):
        self._save_heartbeat_data()

        password = self.init_data.get('password', None)
        confirm = self.init_data.get('confirm', None)
        if password and password == confirm:
            self.object.set_password(password)

        super(HackerAddSerializer, self).save()
        return self.object

    class Meta:
        model = Hacker
        exclude = ['last_login', 'password',
                'is_superuser', 'is_staff',
                'is_active', 'user_permissions',
                'groups', ]
3
  • I used the following, stackoverflow.com/a/18842609/412244, that employs a NestedManyToManyField. Commented Nov 30, 2013 at 19:59
  • Have a look at this answer I wrote on a similar post: Similar post answered Hope it'll help you. Commented May 22, 2014 at 16:02
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented Aug 4, 2019 at 21:37

1 Answer 1

3

Nested Serializers are Readonly at present (out of the box). You have to write your own conversion methods to support writable nested serializers (see docs).

There's work to implement writable nested serializers in the django rest framework project.

Instead, I'd suggest you use a RelatedField (like PrimaryKeyRelatedField or HyperlinkedRelatedField) to reference nested models. You can use the depth option to have these related fields nested when you serialize the data to the client.

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

Comments

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.