I am receiving the error
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'.
when I am trying to write a Django serializer.
My serializer looks like this:
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")
My nested serializer looks like this:
class MedTestGetTestResultSerializer(serializers.ModelSerializer):
class Meta:
model = MedTestsResults
fields = ("result_date",
"value",
"test_started",
"status_code")
Why would I be receiving this error? When I try to use the second serializer in the shell, I get the status_code error when I don't set many=True, but I am setting many=True in the first serializer.
MedTestsResults model:
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)
answer: use 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
And very hard to read you code (style, naming conventions, etc) try two scoops
I think django-rest-framework uses status_code
somewhere, you might want to try renaming your status_code
field to something else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With