将Django Rest Framework与ForeignKey一起使用

时间:2014-03-15 19:43:22

标签: json django django-rest-framework

我正在尝试使用django rest框架来创建Module模型,但我无法正确地获得JSON格式。

我创建了一个讲座和一个实验室,然后尝试用讲座和实验室制作一个模块。但是我收到一个错误(无法添加“”:实例在数据库上“默认”,值在数据库中“无”

以下是我正在尝试使用的JSON内容。

{
    "title": "First Module", 
    "labs": [
    {
        "title": "First Lab", 
        "duration": 10.0, 
        "description": "This is the first lab."
    }
], 
    "lectures": [
    {
        "title": "First Lecture", 
        "duration": 0.0, 
        "description": "This is the first lecture."
    }
]
}


class Lab(models.Model):
    title = models.CharField(max_length=255)
    duration = models.FloatField(help_text=_("Duration of Lab in Hours"), blank=True, null=True)
    description = models.TextField(null=True, blank=True)

    def __unicode__(self):
        return self.title

class Lecture(models.Model):
    title = models.CharField(max_length=255)
    duration = models.FloatField(help_text=_("Duration of Lecture in Hours"), blank=True, null=True)
    description = models.TextField(null=True, blank=True)

    def __unicode__(self):
        return self.title


class Module(models.Model):
    title = models.CharField(max_length=255)
    labs = models.ManyToManyField(Lab, related_name='modules', blank=True, null=True)
    lectures = models.ManyToManyField(Lecture, related_name='modules', blank=True, null=True)

    def __unicode__(self):
        return self.title



class LabSerializer(serializers.ModelSerializer):
    class Meta:
        model = Lab
        fields = ('title', 'duration', 'description')


class LectureSerializer(serializers.ModelSerializer):
    class Meta:
        model = Lecture
        fields = ('title', 'duration', 'description')


class ModuleSerializer(serializers.ModelSerializer):
    labs = LabSerializer(many=True, required=False)
    lectures = LectureSerializer(many=True, required=False)

    class Meta:
        model = Module
        fields = ('title', 'labs', 'lectures')

Traceback:
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  400.             response = self.handle_exception(exc)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  397.             response = handler(request, *args, **kwargs)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/rest_framework/generics.py" in post
  468.         return self.create(request, *args, **kwargs)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/rest_framework/mixins.py" in create
  54.             self.object = serializer.save(force_insert=True)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/rest_framework/serializers.py" in save
  596.             self.save_object(self.object, **kwargs)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/rest_framework/serializers.py" in save_object
  995.                 setattr(obj, accessor_name, object_list)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in __set__
  840.         manager.add(*value)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in add
  581.                 self._add_items(self.source_field_name, self.target_field_name, *objs)
File "/home/siecje/virtualenv/ccafe/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in _add_items
  640.                                                (obj, self.instance._state.db, obj._state.db))

Exception Type: ValueError at /modules/
Exception Value: Cannot add "<Lab: First Lab>": instance is on database "default", value is on database "None"

0 个答案:

没有答案