如何使用 Rest Framework 在 Django 中序列化 OneToOne 关系?
How to serialize a relation OneToOne in Django with Rest Framework?
我有一个 class PersonProfile 和一个 class 人物。
在 class Person 中,我与 PersonProfile 建立了 OneToOne 关系。
我如何序列化它?
class PersonProfile(models.Model):
interests = models.CharField(max_length=200, blank=True)
researchLines = models.CharField(max_length=200, blank=True)
loginName = models.CharField(max_length=50, blank=True)
class Person(models.Model):
profile = models.OneToOneField(PersonProfile)
enviroment = models.ForeignKey(Enviroment, related_name="persons")
您可以创建自定义序列化程序字段来创建您的自定义 PersonSerializer。
您可以添加字段以从 Enviroment
.
获取值
class PersonSerializer(serializers.HyperlinkedModelSerializer):
interests = serializers.CharField(source='profile.interests')
researchLines = serializers.CharField(source='profile.researchLines')
loginName = serializers.CharField(source='profile.loginName')
# --- FIELDS FOR enviroment
class Meta:
model = Person
fields = ('interests', 'researchLines', 'loginName',
#-- enviroment fields
)
您也可以使用serializers.ModelSerializer
class PersonSerializer(serializers.ModelSerializer):
#-------------------------
我最近偶然发现了类似的东西,这就是我序列化与 PersonProfile 具有 OneToOne 关系的 Person 的方法。
class PersonProfileSerializer(serializers.ModelSerializer):
class Meta:
model = PersonProfile
fields = __all__ # in your case since you are using all fields.
class PersonSerializer(serializers.ModelSerializer):
person = PersonProfileSerializer(required=True)
class Meta:
model = Person
fields = __all__
# Now override the default create method to create a new object.
# A similar overriding can be done for update as well.
def create(self, validated_data):
"""
Make necessary modifications as per your requirements
"""
person_profile = PersonProfileSerializer.create(PersonProfileSerializer(), validated_data)
person, created = Person.objects.create(profile=person_profile)
return person
我有一个 class PersonProfile 和一个 class 人物。 在 class Person 中,我与 PersonProfile 建立了 OneToOne 关系。 我如何序列化它?
class PersonProfile(models.Model):
interests = models.CharField(max_length=200, blank=True)
researchLines = models.CharField(max_length=200, blank=True)
loginName = models.CharField(max_length=50, blank=True)
class Person(models.Model):
profile = models.OneToOneField(PersonProfile)
enviroment = models.ForeignKey(Enviroment, related_name="persons")
您可以创建自定义序列化程序字段来创建您的自定义 PersonSerializer。
您可以添加字段以从 Enviroment
.
class PersonSerializer(serializers.HyperlinkedModelSerializer):
interests = serializers.CharField(source='profile.interests')
researchLines = serializers.CharField(source='profile.researchLines')
loginName = serializers.CharField(source='profile.loginName')
# --- FIELDS FOR enviroment
class Meta:
model = Person
fields = ('interests', 'researchLines', 'loginName',
#-- enviroment fields
)
您也可以使用serializers.ModelSerializer
class PersonSerializer(serializers.ModelSerializer):
#-------------------------
我最近偶然发现了类似的东西,这就是我序列化与 PersonProfile 具有 OneToOne 关系的 Person 的方法。
class PersonProfileSerializer(serializers.ModelSerializer):
class Meta:
model = PersonProfile
fields = __all__ # in your case since you are using all fields.
class PersonSerializer(serializers.ModelSerializer):
person = PersonProfileSerializer(required=True)
class Meta:
model = Person
fields = __all__
# Now override the default create method to create a new object.
# A similar overriding can be done for update as well.
def create(self, validated_data):
"""
Make necessary modifications as per your requirements
"""
person_profile = PersonProfileSerializer.create(PersonProfileSerializer(), validated_data)
person, created = Person.objects.create(profile=person_profile)
return person