It might help to see how your models are arranged, but are you trying to 
_create_ a new Log that has a specific group attached to it? Because the 
serializer you've defined doesn't handle that case, I don't think. But, try 
this:

data = {
   "group": {
       "id": group.pk,
       "name": group.name,
   },
   # other relavant fields here
}



On Monday, November 6, 2017 at 10:22:00 PM UTC-6, Gary Ng wrote:
>
> There is a route /group/task, every time it is called, the log will be 
> generated.
>
> This is views.py
>
> class GroupViewSet(viewsets.ViewSet):
>         ....
>         @detail_route(methods=['post'], url_path='task')
>         def get_task(self, request, pk=None):
>                 group = get_object_or_404(Group, pk=pk)
>                 #group = ServerSerializer(group) <--- tried but not work
>
>                 data = {
>                     "group": group,
>                     #"group": group.data, <--- tried but not work
>                 }
>                 log_serializer = LogSerializer(data=data)
>         if log_serializer.is_valid(raise_exception=True):
>             log_serializer.save()
>
>
> This is serializer.py
>
> class GroupSerializer(serializers.ModelSerializer):
> class Meta:
>     model = Group
>     fields = ('id', 'name')
>
> class LogSerializer(serializers.ModelSerializer):
>     group = GroupSerializer()
>     class Meta:
>         model = Log
>         fields = ('group', 'created')
>
>
> The post responese:
>
> {
>     "group": {
>         "non_field_errors": [
>             "Invalid data. Expected a dictionary, but got group."
>         ]
>     }}
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to