これが私のモデルです:
class Address(models.Model):
"""
This is an Adress
"""
address_complete = models.CharField(max_length=100)
door_code = models.CharField(max_length=20, blank=True, null=True)
floor = models.IntegerField(blank=True, null=True)
infos = models.CharField(max_length=100, blank=True, null=True)
class Meta:
verbose_name_plural = "Addresses"
私はserializer.pyでこのためのシリアライザーを作成しました:
from rest_framework import serializers
from party_app.models import Address, UserProfile, Stuff, Event, Bringing, Quantity
class AddressSerializer(serializers.Serializer):
pk = serializers.Field()
address_complete = serializers.CharField(max_length=100)
door_code = serializers.CharField(max_length=20)
floor = serializers.IntegerField()
infos = serializers.CharField(max_length=100)
def restore_object(self, attrs, instance=None):
"""
Create or update a new UserProfile instance.
"""
if instance:
# Update existing instance
instance.address_complete = attrs.get('address_complete', instance.address_complete)
instance.door_code = attrs.get('door_code', instance.door_code)
instance.floor = attrs.get('floor', instance.floor)
instance.infos = attrs.get('infos', instance.infos)
return instance
# Create new instance
return Address(**attrs)
python manage?pyシェルを使用してアドレスをシリアル化しようとすると、次のようになります。
>>> seria = AddressSerializer(Address)
>>> seria.data
AttributeError: type object 'Address' has no attribute 'address_complete'
DjangoRestFrameworkを初めて使用するので、なぜこれを入手したのかわかりません...
何か明らかなことがあれば、教えていただければ幸いです!!