저는 장고를 처음 사용하고 장고 Rest 프레임 워크 튜토리얼을 사용하여 웹 API를 작성하고 있습니다.모델의 새 인스턴스를 만들 때 장고 확인
다음 모델이 있고 createAPIView를 사용하여 각 모델의 새 인스턴스를 만드는보기가 있습니다. 내가하고 싶은 무엇
class ValidationClass (models.Model):
id = models.IntegerField(primary_key=True, null=False)
name = models.CharField(max_length=128, unique=True)
parameters = models.TextField()
def __str__(self):
return '{} - {} - {}'.format(self.id, self.name, self.parameters)
db = 'qa_suite'
class Test(models.Model):
id = models.IntegerField(primary_key=True, null=False)
name = models.CharField(max_length=128, unique=True)
test_group = models.ForeignKey(TestGroup)
description = models.TextField()
query_text = models.TextField()
failure_condition = models.CharField(max_length=20, choices=(
('Error', 'Error'),
('Warn', 'Warn')
), default = 'Error')
status = models.CharField(max_length=20, choices=(
('Enabled', 'Enabled'),
('Disabled', 'Disabled')
), default = 'Enabled')
created = models.DateTimeField('Creation Date', auto_now_add=True, blank=True)
updated = models.DateTimeField('Date last updated', auto_now_add=True, blank=True)
def __str__(self):
return '{} - {}'.format(self.id, self.name)
db = 'qa_suite'
class TestValidation(models.Model):
test = models.ForeignKey(Test)
validation_class = models.ForeignKey(ValidationClass)
input_parameters = models.TextField()
def __str__(self):
return '{} - {} - {}'.format(self.test, self.validation_class, self.input_parameters)
class Meta:
unique_together = (('test', 'validation_class'),)
db = 'qa_suite'
새 테스트 오브젝트가 작성 될 때마다, 그것은 적어도 하나의 ValidationClass 객체에 연결되어 있는지 확인합니다. Test 개체에는 ValidationClass 개체와 1 대 다수의 관계가 있습니다. 즉, 하나의 테스트에는 여러 개의 유효성 검사 클래스 개체가 연결될 수 있습니다. 이 때문에 테스트 용 모델의 유효성 검사 클래스 객체에 외래 키 참조를 추가 할 수 없습니다. 그래서, 본질적으로, 내가하고 싶은 것은, 새로운 Test 객체가 생성 될 때마다 새로운 TestValidation 객체가 생성되도록하고 싶습니다.
조회수 :
# Post new Validation Class Object to database
class ValidationClassCreateAPI(generics.CreateAPIView):
serializer_class = ValidationClassSerializer
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
# Post new Test Validation Object to database
class TestValidationCreateAPI(generics.CreateAPIView):
serializer_class = TestValidationSerializer
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
# Post new Test Object to database
class TestCreateAPI(generics.CreateAPIView):
serializer_class = TestSerializer
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
이 게시물에이 모델을 만드는 데 사용되는 내 의견이 추가되었습니다. 원자 트랜잭션을 사용하는 경우에도 새 테스트 객체가 만들어지면 새 TestValidation 객체를 만들어야합니다. – RajveerParikh
save 메소드를 대체하는 것이 지금 내가 생각할 수있는 최선의 해결책이 될 것입니다. –
또는 데이터베이스 어설 션 및/또는 수동 검사를 수동으로 정의하십시오. –