Home > Blockchain >  Add manytomany field query in cet_or_create method
Add manytomany field query in cet_or_create method

Time:01-12

def create(self, validated_data):
        employer = User.objects.get(id=self.context.get('employer'))
        candidate = User.objects.get(id=self.context.get('candidate_id'))
        report_details_id = ReportDetails.objects.filter(
            id__in=self.context.get('report_details_id'))
        (report_candidate, created) = ReportCandidate.objects.get_or_create(
            reported_by=employer.id, candidate_id=candidate.id,report_details_id=report_details_id)
        return report_candidate

Here the report_details_id is a manytomanyfield query. How will apply each of the data in it in the get_or_create method?

The QuerySet value for an exact lookup must be limited to one result using slicing.

I'm getting this error from the above code. Thanks in Advance.

CodePudding user response:

report_details_id contain a list of objects thats why you are getting the above error. to fix the issue you just need to use in operator.

Like :-

   reported_by=employer.id, candidate_id=candidate.id,report_details_id__in=report_details_id)
  •  Tags:  
  • Related