Reputation: 1
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', ]
Upvotes: 0
Views: 1251
Reputation: 8981
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.
Upvotes: 3