Skip to content

Instantly share code, notes, and snippets.

@eugene-goldberg
Created January 27, 2015 20:59
Show Gist options
  • Save eugene-goldberg/426dcca9ff98ec33ff0a to your computer and use it in GitHub Desktop.
Save eugene-goldberg/426dcca9ff98ec33ff0a to your computer and use it in GitHub Desktop.
django rest framework
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment