Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable creating nested objects in django rest framework

Hi,

Can you help me how to disable creating nested objects ?

I have serializers like this:
(Employee has ForeignKey to Team)

class TeamSerializer(serializers.ModelSerializer):
    class Meta:
        model = Team
        fields = ('id', 'name')
class EmployeeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Employee
        fields = ('id', 'name', 'surname', 'team')
        depth = 1

or instead could be:

class EmployeeSerializer(serializers.ModelSerializer):
    team = TeamSerializer()
    class Meta:
        model = Employee
        fields = ('id', 'name', 'surname', 'team')

when i post json (create employee)

{
    name: "name",
    surname: "surname",
    team: {
           id: 1,
           name: "Web Team"
          }
}

object employee creates but also object team... is there any way to disable creating team object together with employee ? i just want to create employee and set selected team (curently in database) to employee

And on GET (list) i would like to be able to retrieve data like:

{
 name: "name",
 surname: "surname",
 team: {
    id: 1,
    name: "Web Team"
 }

not like that

{
 name: "name",
 surname: "surname",
 team: 1
}

Is there any way to to that in django rest framework (also iam using angular)
Regards

UPDATE

Currently serializers:

class TeamSerializer(serializers.ModelSerializer):
    class Meta:
        model = Team

class EmployeeSerializer(serializers.ModelSerializer):
    team = TeamSerializer()

    class Meta:
        model = Employee

I cant use serializers.RelatedField() because it's returned only unicode I need 'id' and 'name' (i suppose)

this is my POST json: ( Restangular.all('employee').post(data) )

data: {
        name: "emp1",
        photo: "",
        skype: "",
        surname: "qweqwe",
        team: {
              id: 1,
              name: "Web",
        }
}

and DRF returned json:

employee: {
    id: 2,
    name: "emp1",
    photo: "",
    skype: "",
    surname: "qweqwe",
    team: {
        id:3, <-- NEW ID!
        name: "Web"
    }   
}

so yea, i am sure that new team objects created. So what now ?:)

like image 499
user3900778 Avatar asked Aug 01 '14 20:08

user3900778


People also ask

How do I use Mixins in Django REST framework?

mixins override dispatch if they need to check if user if logged in, has permission. This is one of the first methods called and view determines which request method (get, post etc) is used. mixins override get_context_data() to add new data to context. This method passes keyword arguments to the template context.

What is to_ representation?

to_representation(self, value) method. This method takes the target of the field as the value argument, and should return the representation that should be used to serialize the target. The value argument will typically be a model instance.

What is basename in django?

basename - The base to use for the URL names that are created. If unset the basename will be automatically generated based on the queryset attribute of the viewset, if it has one. Note that if the viewset does not include a queryset attribute then you must set basename when registering the viewset.

How do you serialize an object list in Django REST framework?

Serializing multiple objects To serialize a queryset or list of objects instead of a single object instance, you should pass the many=True flag when instantiating the serializer. You can then pass a queryset or list of objects to be serialized.


2 Answers

I found that ModelSerializer.to_native() and ModelSerializer.from_native() don't exist in the latest version of DRF. I came up with the following derived from the accepted solution:

class PlayerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Player
        fields = ('id', 'name', 'team')

    def to_internal_value(self, data):
        # If team is not a dict, such as when submitting via the Browseable UI, this would fail.
        try:
            data['team'] = data['team']['id']
        except TypeError:
            pass
        return super(PlayerSerializer, self).to_internal_value(data)

    def to_representation(self, instance):
        return ReadPlayerSerializer(instance).data


class ReadPlayerSerializer(serializers.ModelSerializer):
    team = TeamSerializer()

    class Meta(PlayerSerializer.Meta):
        pass

Effectively, it seems that from_native is now to_representation, and to_native is no to_internal_value

like image 56
DanH Avatar answered Sep 30 '22 16:09

DanH


Okay, first of all, are you sure that your nested object is created? Because DRF was not designed to create nested objects, so this is a very strange behavior (more precisely, this is a work in progress, as stated by its creator, Tom Christie).

Then, in order to have the serializer representation that you want, you must follow some rules:

  1. Create a simple serializer for each model (just like in your first code snippet)

  2. Add the FK relationship on the EmployeeSerializer: (be careful, for this to work, you must have your FK named 'team') team = serializers.RelatedField()

  3. Add the ManyToOne relationship on your TeamSerializer: (this is only needed if you also want to have all your employees when you get a Team object; if not, you can skip this part) employees = EmployeeSerializer(required=False, many=True)

Also, you should remove the depth attribute from your serializer, he is the one that flattens your serialization (or you can just set it to 2). Hope this helps.

UPDATE

View for multiple serializers:

def get_serializer_class(self):
    if self.request.method == 'GET':
        return ReadEmployeeSerializer
    elif self.request.method == 'POST':
        return WriteEmployeeSerializer
    else:
        return DefaultSerializer

class WriteEmployeeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Employee
        fields = ('id', 'name', 'surname', 'team')

class ReadEmployeeSerializer(serializers.ModelSerializer):
    team = TeamSerializer()
    class Meta:
        model = Employee
        fields = ('id', 'name', 'surname', 'team')

A bit redundant, but should do the job.

like image 44
AdelaN Avatar answered Sep 30 '22 17:09

AdelaN