我收到错误
AttributeError: Got AttributeError when attempting to get a value for field `status_code` on serializer `MedTestGetTestResultSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `unicode` instance.
Original exception text was: 'unicode' object has no attribute 'status_code'.
当我尝试编写Django序列化程序时。
我的序列化器看起来像这样:
class MedTestGetTestSerializer(serializers.ModelSerializer):
test_id = serializers.CharField(source='medteststypetest_id.test_id')
test_name = serializers.CharField(source='medteststypetest_id.test_name')
username = serializers.CharField(source='user_id.username')
user_id = serializers.CharField(source='user_id.user_id')
kit_id = serializers.CharField(source='kit_id.kit_id')
test_results = MedTestGetTestResultSerializer(source='medtests_id', many=True, required=False)
class Meta:
model = MedTests
fields = ("medtests_id",
"datekitord",
"testdate",
"user_id",
"kit_id",
"status",
"username",
"test_name",
"test_id",
"test_results")
我的嵌套序列化程序如下所示:
class MedTestGetTestResultSerializer(serializers.ModelSerializer):
class Meta:
model = MedTestsResults
fields = ("result_date",
"value",
"test_started",
"status_code")
为什么我会收到此错误?当我尝试在shell中使用第二个序列化程序时,当我没有设置many = True时,我得到status_code错误,但是我在第一个序列化程序中设置了many = True。
MedTestsResults模型:
class MedTestsResults(models.Model):
medtests_results_id = models.CharField(max_length=100)
medtests_id = models.ForeignKey(MedTests)
result_date = models.DateTimeField(null=True, blank=True)
test_started = models.DateTimeField(null=True, blank=True)
status_code = models.CharField(max_length=25)
value = models.IntegerField(null=True, blank=True)
hiv4g_rsltdate = models.DateTimeField(null=True, blank=True)
hivms_rsltdate = models.DateTimeField(null=True, blank=True)
hiv4gpos = models.CharField(max_length=100, null=True, blank=True)
hiv4gneg = models.CharField(max_length=100, null=True, blank=True)
hiv4gind = models.CharField(max_length=100, null=True, blank=True)
hiv4grej = models.CharField(max_length=100, null=True, blank=True)
hivmpos = models.CharField(max_length=100, null=True, blank=True)
hivmsneg = models.CharField(max_length=100, null=True, blank=True)
hivmsind = models.CharField(max_length=100, null=True, blank=True)
hivmsrej = models.CharField(max_length=100, null=True, blank=True)
MedTests:
class MedTests(models.Model):
medtests_id = models.CharField(max_length=100)
medteststypetest_id = models.ForeignKey(MedTestsType)
datekitord = models.DateTimeField()
testdate = models.DateTimeField()
status = models.CharField(max_length=100)
hiv4g_testdate = models.DateTimeField(blank=True, null=True)
hivms_testdate = models.DateTimeField(blank=True, null=True)
kit_id = models.ForeignKey(Kits)
user_id = models.ForeignKey(User)
答案 0 :(得分:1)
回答:使用related_name attr
models.py
class MedTest(models.Model):
date_kit_ord = models.DateTimeField()
test_date = models.DateTimeField()
status = models.CharField(max_length=100)
hiv4g_test_date = models.DateTimeField(blank=True, null=True)
hivms_test_date = models.DateTimeField(blank=True, null=True)
user = models.ForeignKey(User)
class MedTestResult(models.Model):
med_test_result = models.CharField(max_length=100)
med_test = models.ForeignKey(MedTest, related_name='med_tests')
result_date = models.DateTimeField(null=True, blank=True)
test_started = models.DateTimeField(null=True, blank=True)
status_code = models.CharField(max_length=25)
value = models.IntegerField(null=True, blank=True)
hiv4g_rsltdate = models.DateTimeField(null=True, blank=True)
hivms_rsltdate = models.DateTimeField(null=True, blank=True)
hiv4gpos = models.CharField(max_length=100, null=True, blank=True)
hiv4gneg = models.CharField(max_length=100, null=True, blank=True)
hiv4gind = models.CharField(max_length=100, null=True, blank=True)
hiv4grej = models.CharField(max_length=100, null=True, blank=True)
hivmpos = models.CharField(max_length=100, null=True, blank=True)
hivmsneg = models.CharField(max_length=100, null=True, blank=True)
hivmsind = models.CharField(max_length=100, null=True, blank=True)
hivmsrej = models.CharField(max_length=100, null=True, blank=True)
tracing.py
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'youproject.settings')
django.setup()
from rest_framework import serializers
from frontend import models as fm
class MedTestResultSerializer(serializers.ModelSerializer):
class Meta:
model = fm.MedTestResult
fields = ('result_date',
'value',
'test_started',
'status_code')
class MedTestSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
user_id = serializers.CharField(source='user.id')
med_tests = MedTestResultSerializer(many=True)
class Meta:
model = fm.MedTest
fields = (
'med_tests',
'user_id',
'username',
)
print MedTestSerializer(fm.MedTest.objects.all()[0]).data
非常难以阅读代码(样式,命名约定等),请尝试two scoops
答案 1 :(得分:1)
我认为django-rest-framework在某处使用status_code
,您可能想尝试将status_code
字段重命名为其他字段。
答案 2 :(得分:0)
在我看来,问题可能出现在视图中。如果您在视图中定义错误的queryset
,则会收到此错误。例如
class YourViewSet(ModelViewSet):
queryset = WrongModel.objects.all()
serializer_class = YourSerializer
如果您为queryset
值使用的模型与序列化程序中定义的模型不同,则会收到该错误。