我正在尝试更新我的一个模型(这是一个嵌套模型 - 实际上你可以看到三个级别),我收到以下错误:
AssertionError:默认情况下,.update()方法不支持可写的嵌套字段 . 为序列化程序SystemSettingsSerializer编写显式的.update()方法,或在嵌套的序列化程序字段上设置read_only = True .
我一直在阅读有关嵌套模型和嵌套序列化器的一整天,尝试将 update
和 create
方法设置为 read_only=True
但是无论我做了什么,它都无法正常工作:( :(
这些是我的模特:
class SystemSettings(models.Model):
# ... some fields
class Components(models.Model):
settings = models.ForeignKey(SystemSettings, related_name="Components")
class SysComponent(models.Model):
class Meta:
abstarct = True
index = models.PositiveIntegerField(primery_key=True)
is_active = models.BooleanField(default=False)
component = NotImplemented
class Foo(SysComponent):
component = models.ForeignKey(Components, related_name="Foo")
class Bar(SysComponent):
component = models.ForeignKey(Components, related_name="Bar")
task_id = models.PositiveIntegerField(default=0)
和序列化器:
class SystemSettingsSerializer(ModelSerializer):
Components = ComponentsSerializer(many=True)
class Meta:
model = SystemSettings
fields = [# some fields,
Components]
class ComponentsSerializer(ModelSerializer):
Foo = FooSerializer(many=True)
Bar = BarSerializer(many=True)
class Meta:
model = Components
fields = ['Foo',
'Bar']
class FooSerializer(ModelSerializer):
class Meta:
model = Foo
class BarSerializer(ModelSerializer):
class Meta:
model = Bar
我的逻辑如下:我通过 GET
获取 SystemSettings
并以表格形式显示 . 用户可以根据需要更改它,点击提交后我会通过 PUT
将其发回 .
正如我所说,点击提交后我收到了上述错误 . 任何帮助,将不胜感激 .
EDIT: I am using django 1.7.8 by the way