1👍
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
1👍
I think django-rest-framework uses status_code
somewhere, you might want to try renaming your status_code
field to something else.
- Pytest and Django settings runtime changes
- TypeError at / __init__() takes exactly 1 argument (2 given)
- Not able to start `django` project in local as well as in docker
0👍
The issues might be in the view like in my case. If you define the wrong queryset
in your view you will get this error. e.g
class YourViewSet(ModelViewSet):
queryset = WrongModel.objects.all()
serializer_class = YourSerializer
If you use a different model for your queryset
value from the one defined in your serializer, you will get that error.
Source:stackexchange.com