我的序列化程序不会抛出错误,但是每当我尝试使用serializer.data时,我都会遇到AttributeError:' str'对象没有属性' _meta'。我做了一个设定的跟踪并确认类型是联系。
File "/test/user_profile/views.py", line 97, in retrieve
return Response(serializer.data)
File "/home/will/virtualenvs/generic_api/lib/python3.5/site-packages/rest_framework/serializers.py", line 531, in data
ret = super(Serializer, self).data
File "/home/will/virtualenvs/generic_api/lib/python3.5/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/home/will/virtualenvs/generic_api/lib/python3.5/site-packages/rest_framework/serializers.py", line 483, in to_representation
fields = self._readable_fields
File "/home/will/virtualenvs/generic_api/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/will/virtualenvs/generic_api/lib/python3.5/site-packages/rest_framework/serializers.py", line 377, in _readable_fields
field for field in self.fields.values()
File "/home/will/virtualenvs/generic_api/lib/python3.5/site-packages/rest_framework/serializers.py", line 363, in fields
for key, value in self.get_fields().items():
File "/home/will/virtualenvs/generic_api/lib/python3.5/site-packages/rest_framework/serializers.py", line 983, in get_fields
info = model_meta.get_field_info(model)
File "/home/will/virtualenvs/generic_api/lib/python3.5/site-packages/rest_framework/utils/model_meta.py", line 42
----------
, in get_field_info
reverse_relations = _get_reverse_relationships(opts)
File "/home/will/virtualenvs/generic_api/lib/python3.5/site-packages/rest_framework/utils/model_meta.py", line 146, in _get_reverse_relationships
not get_remote_field(relation.field).through._meta.auto_created
AttributeError: 'str' object has no attribute '_meta'
这是我的模特
class Contact(models.Model):
SUBSCRIPTION_LEVEL = {
0: 'Both',
1: 'Monitoring Event',
2: 'DVR Down'
}
id = models.AutoField(primary_key=True, db_column='Id')
user = models.ForeignKey(User, db_column='UserId')
title = models.CharField(default='', max_length=20, blank=True, db_column='Title')
email_one = models.CharField(default='', max_length=75, blank=True, db_column='EmailOne')
email_two = models.CharField(default='', max_length=75, blank=True, db_column='EmailTwo')
subscription_level = models.IntegerField(default=0, blank=True, db_column='SubscriptionLevel')
phone_one = models.CharField(default='', max_length=15, blank=True, db_column='PhoneOne')
phone_two = models.CharField(default='', max_length=15, blank=True, db_column='PhoneTwo')
company = models.CharField(default='', max_length=50, blank=True, db_column='Company')
notes = models.CharField(default='', max_length=500, blank=True, db_column='Notes')
vehicle_description = models.CharField(default='', max_length=200, blank=True, db_column='VehicleDescription')
vehicle_plates = models.CharField(default='', max_length=20, blank=True, db_column='VehiclePlates')
is_active = models.BooleanField(default=True, blank=False, db_column='IsActive')
这是视图
class ContactViewSet(ModelViewSet):
permission_classes = (permissions.IsAuthenticated,)
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
serializer_class = ContactSerializer
def retrieve(self, request, pk=None):
queryset = Contact.objects.filter(is_active=True)
if not self.request.user.is_staff:
queryset = queryset.filter(contacts__user_id=self.request.user.id)
contact = get_object_or_404(queryset, id=pk)
serializer = ContactSerializer(contact)
return Response(serializer.data)
我的序列化器
class ContactSerializer(serializers.ModelSerializer):
first_name = serializers.CharField(source='user.first_name', required=False)
last_name = serializers.CharField(source='user.last_name', required=False)
class Meta:
model = Contact
fields = ['id', 'user', 'first_name', 'last_name', 'title', 'email_one', 'email_two', 'company', 'notes', 'subscription_level',
'phone_one', 'phone_two', 'vehicle_description', 'vehicle_plates', 'is_active']
我一直在玩串行器,甚至删除字段或只设置字段=' 所有'但我还没有弄清楚为什么会这样。
答案 0 :(得分:0)
代码对我来说很好看。 不知何故,似乎序列化程序无法获得反转的多对多或外键关系。 你有没有设法发现错误的领域?