我有两个模型和两个序列化器。我正在尝试使用其中一个模型的属性。
我已经尝试使用foo = serializers.ReadOnlyField()
,但是不起作用。
我的例子:
class ExampleOne(models.Model):
example_three = models.ForeignKey(
"foo.ExampleThree",
on_delete=models.CASCADE,
related_name="ones",
)
@property
def foo(self):
return bar
class ExampleTwo(models.Model):
example_four = models.ForeignKey(
"foo.ExampleFour",
on_delete=models.CASCADE,
related_name="twos",
)
class ExampleThree(models.Model):
example_four = models.ForeignKey(
"foo.ExampleFour",
on_delete=models.CASCADE,
related_name="threes",
)
class ExampleFour(models.Model):
...
fields=...
...
class ExampleOneSerializer(serializers.Serializer):
class Meta:
model = ExampleOne
fields = "__all__"
class ExampleTwoSerializer(serializers.Serializer):
foo = serializers.ReadOnlyField()
class Meta:
model = ExampleTwo
fields = "__all__"
有人知道我如何从另一个序列化程序中获得这个功能吗?