DRF更新插入许多ForeignKey关系模型



我有六种型号,它们如下:

class Certificate(DateTimeLog):
name = models.TextField(max_length=255)

class Vacancy(DateTimeLog):
name = models.CharField(max_length=255)
parent_position = models.ForeignKey("self", on_delete=models.CASCADE, null=True, blank=True,
related_name='sub_positions')

class Region(DateTimeLog):
name = models.CharField(max_length=255)

class MaritalStatus(DateTimeLog):
name = models.CharField(max_length=255)

class Candidate(DateTimeLog):
pin = models.CharField(max_length=16, unique=True)
first_name = models.CharField(max_length=64, blank=True, null=True)
last_name = models.CharField(max_length=64, blank=True, null=True)
marital_status = models.ForeignKey(MaritalStatus, on_delete=models.SET_NULL, null=True, blank=True)
certificate = models.ManyToManyField(Certificate, blank=True)

class Candidacy(DateTimeLog):
candidate = models.ForeignKey(Candidate, on_delete=models.CASCADE)
vacancy = models.ForeignKey(Vacancy, on_delete=models.CASCADE)
work_region = models.ForeignKey(Region, on_delete=models.SET_NULL, null=True, blank=True)

现在我想处理的是,如果存在候选记录(我正在用引脚进行检查(,然后检查并更新候选相关数据。如果候选人不存在,创建它。创建或更新候选项后,将其分配给候选项。我的序列化程序如下所示:

class CandidateSerializer(serializers.ModelSerializer):
marital_status = MaritalStatusSerializer(required=False)
certificate = CertificateSerializer(many=True, required=False)
def create(self, validated_data):
marital_status_data = validated_data.pop("marital_status")
certificate_data = validated_data.pop("certificate")
candidate = Candidate.objects.create(**validated_data)
ms_o = MaritalStatus.objects.get(name=marital_status_data["name"])
candidate.marital_status = ms_o
for certificate in certificate_data:
certificate_o = Certificate.objects.create(**certificate)
candidate.certificate.add(certificate_o)
candidate.save()
return candidate
class Meta:
model = Candidate
fields = '__all__'
depth = 1

class CandidacySerializer(serializers.ModelSerializer):
candidate = CandidateSerializer()
vacancy = VacancySerializer()
work_region = RegionSerializer()
def create(self, validated_data):
candidate_s = CandidateSerializer()
candidate_data = validated_data.pop('candidate')
vacancy_data = validated_data.pop('vacancy')
work_region_data = validated_data.pop('work_region')
vac_o = Vacancy.objects.get(name=vacancy_data['name'])
wr_o = Region.objects.get(name=work_region_data['name'])
candidate_o = candidate_s.create(validated_data=candidate_data)
validated_data.update({
'candidate': candidate_o,
'vacancy': vac_o,
'work_region': wr_o
})
candidacy = Candidacy.objects.create(**validated_data)
return candidacy
class Meta:
model = Candidacy
fields = '__all__'
depth = 1

目前,我可以通过帖子请求创建它。我需要明确地检查它吗?还是可以在序列化程序中实现它?

首先,您可以在CandidateSerializer中使用update_or_create,如下所示:

class CandidateSerializer(serializers.ModelSerializer):
marital_status = MaritalStatusSerializer(required=False)
certificate = CertificateSerializer(many=True, required=False)
def create(self, validated_data):
marital_status_data = validated_data.pop("marital_status")
certificate_data = validated_data.pop("certificate")
candidate, created = Candidate.objects.update_or_create(
pin=validated_data['pin'],
**validated_data
)
ms_o = MaritalStatus.objects.get(name=marital_status_data["name"])
candidate.marital_status = ms_o
for certificate in certificate_data:
certificate_o = Certificate.objects.create(**certificate)
candidate.certificate.add(certificate_o)
candidate.save()
return candidate
class Meta:
model = Candidate
fields = '__all__'
depth = 1

其次,您不应该直接调用方法create()。您必须使用save()。您必须更换:

candidate_s = CandidateSerializer()
candidate_o = candidate_s.create(validated_data=candidate_data)

通过

candidate_s = CandidateSerializer(data=candidate_data)
candidate_o = candidate_s.save()

您可以在此处阅读有关保存实例的更多信息。

这样您就有了一个更直接的选项。有一个名为get或create的函数,它将检查对象是否存在,并返回它,否则将生成它。类似的问题可以在这里找到同一问题的另一个用法顺便说一句,如果这个pin是敏感的,我建议加密它。

由于您已经使用使pin独一无二

pin = models.CharField(max_length=16, unique=True)

不可能有两个类似的引脚。现在,要根据pin检查候选人,您必须在创建方法中手动执行类似操作。

if Candidate.objects.filter(pin = revievedPinData):
here candidate is already present so update the information information
and again check if Canditate is in Candidacy or not
first get the candidate object
c = Candidate.objects.get(pin = revievedPinData)
then
if Candidacy.objects.filter(candidate = c):
if true candidate is already assigned and the updated value of candidate will reflect here
else: create the Candidacy with the current candidate and other values
else:
if candidate is not there (from the given pin)
create a new candidate
aa = candidate.objects.create()
create a new Candidacy with candidate aa
Candidacy.objects.create(candidate=aa, **otherdata)


相关内容

  • 没有找到相关文章

最新更新