Ejemplo n.º 1
0
    def create(self, validated_data):
        usages = validated_data.pop('usages', [])
        instance = super(PrefixSerializer, self).create(validated_data)
        for usage in usages:
            manage.PrefixUsage(prefix=instance, usage=usage).save()

        return instance
Ejemplo n.º 2
0
    def update(self, instance, validated_data):
        if 'usages' in validated_data:
            new_usages = set(u.id for u in validated_data.pop('usages'))
            current_usages = set(u.id for u in instance.usages.all())
            to_add = new_usages - current_usages
            to_delete = current_usages - new_usages

            for usage in to_add:
                manage.PrefixUsage(
                    prefix=instance,
                    usage=manage.Usage.objects.get(pk=usage)).save()

            manage.PrefixUsage.objects.filter(
                prefix=instance, usage__in=list(to_delete)).delete()
        return super(PrefixSerializer, self).update(instance, validated_data)