@Eugene Did you get any update about this question? I am also eager to know 
more about it as I have similar situation in one of my project. I would 
also like to know how to check the ownership of the parent object against 
current user. For example, in context of your question, how you would check 
the current user who is adding Workloads to the Blueprint, if they have the 
permission to access/add/edit workloads in it.

On Tuesday, January 27, 2015 at 9:53:41 PM UTC+1, Eugene Goldberg wrote:
>
> I have the following two models: Blueprint and Workload.
> The Blueprint model instance should have a collection of Workloads 
> associated with it.
> Initially, bot the Blueprint and Workload instances should be allowed to 
> be created independently.
> A complete typical scenario as follows:
>
> 1) a new Blueprint instance is created
> 2) a new Workload instance is created
> 3) the created Workload instance is added to the Blueprint's 'workloads' 
> collection
>
> The python REPL version is this:
>
> blueprint = Blueprint(name="bluepint 1")
> blueprint.save()
> workload = Workload)name="workload 1")
> workload.save()
> blueprint.workloads.add(blueprint)
> blueprint.save()
>
> In my python client, I am able to create the instances of the Blueprint 
> and Workload independently, and without issue.
>
> My question is: what is the proper HTTP method and the corresponding URL 
> syntax, to add a Workload to the Blueprint's 'workloads' collection.
>
> Here is the models.py:
>
> class Workload(models.Model):
>     name = models.CharField(max_length=120, blank=True, default='')
>     description = models.TextField()
>     image = models.CharField(max_length=120, blank=True, default='')
>     flavor = models.CharField(max_length=120, blank=True, default='')
>     blueprint = models.ForeignKey('Blueprint', related_name='workloads', 
> null=True)
>
>     class Meta:
>         ordering = ('name',)
>         unique_together = ('blueprint', 'name')
>
>     def __unicode__(self):
>         return '%d: %s' % (self.name, self.description)
>
>
> class Blueprint(models.Model):
>     name = models.CharField(max_length=120, blank=True, default='')
>     description = models.TextField()
>
>     class Meta:
>         ordering = ('name',)
>
>
> And here is the serializers.py:
>
> # region Workload Serializer
> class WorkloadSerializer(serializers.Serializer):
>     pk = serializers.IntegerField(read_only=True)
>     name = serializers.CharField(required=False, allow_blank=True, 
> max_length=120)
>     description = serializers.CharField(style={'type': 'textarea'})
>     image = serializers.CharField(required=False, allow_blank=True, 
> max_length=120)
>     flavor = serializers.CharField(required=False, allow_blank=True, 
> max_length=120)
>
>
>     def create(self, validated_data):
>         """
>         Create and return a new `Snippet` instance, given the validated data.
>         """
>         return Workload.objects.create(**validated_data)
>
>     def update(self, instance, validated_data):
>         """
>         Update and return an existing `Snippet` instance, given the validated 
> data.
>         """
>         instance.name = validated_data.get('name', instance.name)
>         instance.description = validated_data.get('description', 
> instance.description)
>         instance.image = validated_data.get('image', instance.image)
>         instance.flavor = validated_data.get('flavor', instance.flavor)
>         instance.save()
>         return instance
> # endregion
>
>
> # region Blueprint Serializer
> class BlueprintSerializer(serializers.ModelSerializer):
>     workloads = serializers.StringRelatedField(many=True, required=False)
>     pk = serializers.IntegerField(read_only=True)
>     name = serializers.CharField(required=False, allow_blank=True, 
> max_length=120)
>     description = serializers.CharField(style={'type': 'textarea'})
>
>     def create(self, validated_data):
>         """
>         Create and return a new `Snippet` instance, given the validated data.
>         """
>         return Blueprint.objects.create(**validated_data)
>
>     def update(self, instance, validated_data):
>         """
>         Update and return an existing `Snippet` instance, given the validated 
> data.
>         """
>         instance.name = validated_data.get('name', instance.name)
>         instance.description = validated_data.get('description', 
> instance.description)
>         instance.save()
>         return instance
>
>     class Meta:
>         model = Blueprint
>         fields = ('name', 'description', 'workloads')
> # endregion
>
> -Eugene
>
>
>

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to