Created
April 1, 2013 19:54
-
-
Save toastdriven/5287258 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tastypie.resources import ModelResource | |
from whatever.models import Exercise, Set, AssignedExercise | |
class SetResource(ModelResource): | |
class Meta: | |
queryset = Set.objects.all() | |
def obj_create(self, bundle, **kwargs): | |
# First, rip out the exercise ids so as not to cause a field-list | |
# validation error. | |
exercise_ids = bundle.data.pop('exercises', []) | |
# Call ``super(..)`` to create the new object. | |
new_set = super(SetResource, self).obj_create(bundle, **kwargs) | |
# Create the ``AssignedExercise`` objects. | |
for exercise_id in exercise_ids: | |
try: | |
exercise = Exercise.objects.get(pk=exercise_id) | |
assigned = AssignedExercise.objects.create( | |
# Why on earth isn't ``set`` a ``ForeignKey`` instead of | |
# an ``IntegerField``? | |
sets=new_set.pk, | |
exercise=exercise | |
# Maybe fields initialized here as needed? | |
) | |
except Exercise.DoesNotExist: | |
# Do something sane for your application here. | |
# Perhaps delete the new set & throw an error, or silently | |
# ignore as this code does. | |
pass | |
return new_set |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment