我正在尝试对数据库执行简单的get()post()。我有现有的数据库,并且使用了inspectdb从中生成models.py。我在为模型设置复合主键时遇到问题,其中一部分是外键
模型TestCase与TestSuite模型具有外键关系。 TestCase api的get()效果很好。但是,发布失败并出现以下错误:
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/usr/local/lib/python3.6/dist- packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/views/decorators/csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/viewsets.py" in view
116. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py" in dispatch
495. response = self.handle_exception(exc)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py" in handle_exception
455. self.raise_uncaught_exception(exc)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py" in dispatch
492. response = handler(request, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/mixins.py" in create
20. serializer.is_valid(raise_exception=True)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in is_valid
236. self._validated_data = self.run_validation(self.initial_data)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in run_validation
434. value = self.to_internal_value(data)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py" in to_internal_value
491. validated_value = field.run_validation(primitive_value)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/relations.py" in run_validation
160. return super(RelatedField, self).run_validation(data)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/fields.py" in run_validation
536. value = self.to_internal_value(data)
File "/usr/local/lib/python3.6/dist-packages/rest_framework/relations.py" in to_internal_value
263. return self.get_queryset().get(pk=data)
File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py" in get
407. (self.model._meta.object_name, num)
Exception Type: MultipleObjectsReturned at /dqf_api/test_case/
Exception Value: get() returned more than one TestSuite -- it returned
2!
Request information:
USER: AnonymousUser
可浏览的api调用也因上述错误而失败
models.py
class TestCase(models.Model):
team_name = models.ForeignKey(TestSuite, on_delete=models.DO_NOTHING, db_column='team_name')
suite_name = models.ForeignKey(TestSuite, on_delete=models.DO_NOTHING, db_column='suite_name')
case_name = models.CharField(max_length=100, primary_key=True)
test_type = models.ForeignKey(TestCaseType, on_delete=models.DO_NOTHING)
class Meta:
managed = False
db_table = 'test_case'
unique_together = (('team_name', 'suite_name', 'case_name'),)
class TestSuite(models.Model):
team_name = models.ForeignKey(Team, on_delete=models.DO_NOTHING, db_column='team_name')
suite_name = models.CharField(max_length=100, primary_key=True)
description = models.CharField(max_length=200, blank=True, null=True)
#Additional fields
class Meta:
managed = False
db_table = 'test_suite'
unique_together = (('team_name', 'suite_name'),)
class TestCaseType(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=100)
description = models.CharField(max_length=200, blank=True, null=True)
class Meta:
managed = False
db_table = 'test_case_type'
class Team(models.Model):
team_name = models.CharField(primary_key=True, max_length=30)
description = models.CharField(max_length=100, blank=True, null=True)
class Meta:
managed = False
db_table = 'team'
views.py
class TestCaseViewSet(viewsets.ModelViewSet):
queryset = models.TestCase.objects.all()
serializer_class = serializers.TestCaseSerializer
class TestSuiteViewSet(viewsets.ModelViewSet):
queryset = models.TestSuite.objects.all()
serializer_class = serializers.TestSuiteSerializer
serializers.py
class TestSuiteSerializer(serializers.ModelSerializer):
class Meta:
model = models.TestSuite
fields = '__all__'
class TestCaseSerializer(serializers.ModelSerializer):
class Meta:
model = models.TestCase
fields = '__all__'
data.json
{
"case_name": "NA",
"description": "NA",
"run_level": 1,
"stop_on_failure": false,
"team_name": "ede",
"suite_name": "dummy",
"test_type": 1
}
test_suite表中仅存在一条team_name = ede和suite_name = dummy的记录
我是Django和DRF的新手。任何帮助表示赞赏。