I have a model class named Blueprint:
class Blueprint(models.Model):
name = models.CharField(max_length=120)
description = models.TextField()
workloads = models.CharField(choices=WORKLOAD_CHOICES)
class Meta:
ordering = ["name", ]
This model has children named workloads.
Being totally new to both, django and tastypie, I have this question:
1) Where do I execute the logic, which retrieves the list of workloads, and populates the WORKLOAD_CHOICES: in the models.py (as part of the init) or in the api.py as part of def obj_get ?
P.S. Here is the api.py:
class BlueprintResource(ModelResource):
def obj_create(self, bundle, request=None, **kwargs):
return super(BlueprintResource, self).obj_create(bundle, request)
def obj_update(self, bundle, request=None, **kwargs):
blueprint = Blueprint.objects.get(id=kwargs.get("pk"))
blueprint.description = bundle.data.get("description")
blueprint.name = bundle.data.get("name")
blueprint.workloads = bundle.data.get("workloads")
blueprint.save()
def obj_delete(self, bundle, **kwargs):
return super(BlueprintResource, self).obj_delete(bundle)
class Meta:
queryset = Blueprint.objects.all()
resource_name = 'blueprint'
authorization=Authorization()
Have a look at build_bundle() and full_dehydrate(). I haven't tested this but maybe something like this would work.
from my_app.models import WORKLOAD_CHOICES
class BlueprintResource(ModelResource):
def full_dehydrate(self, bundle, for_list=False):
dic = dict([WORKLOAD_CHOICES])
bundle.data['foo'] = self.build_bundle(data=dic)
return super(BlueprintResource, self).full_dehydrate(bundle, for_list)