title: get all many to many field values + the rest of the model fields in a general query to db
body: <p>I am studying django and I want to do an API endpoint that brings all the employees in the database with all the fields, including the languages field which has a manyToMany relationship to Languages Model.</p>
  <pre><code>class Employee(models.Model):
      name = models.CharField(max_length=50)
      lastname = models.CharField(max_length=50)
      salary = models.PositiveBigIntegerField()
      position = models.ForeignKey(Position, on_delete=models.CASCADE, related_name='employees')
      languages = models.ManyToManyField(Languages, related_name='employees')
  
      def __str__(self):
          return f'{self.name} {self.lastname}'
  
  class Languages(models.Model):
      name = models.CharField(max_length=50)
  
      def __str__(self):
          return self.name
  </code></pre>
  <p>I managed to do this but with a query that gets only one employee</p>
  <pre><code>            try:
                  emp = Employee.objects.get(id=id)
                  data = {
                      'name': emp.name,
                      'lastname': emp.lastname,
                      'salary': emp.salary,
                      'position': emp.position.name,
                      'languages': [x['name'] for x in emp.languages.all().values()]
                  }
              except ObjectDoesNotExist:
                  return JsonResponse({&quot;message&quot;: &quot;No company found&quot;})
              except Exception as ex:
                  return JsonResponse({&quot;message&quot;: &quot;Something went wrong.&quot;})
              return JsonResponse({&quot;message&quot;: &quot;success&quot;, &quot;data&quot;: data})
  </code></pre>
  <p>But now I can't do the same with a query such as Employee.objects.all() or Employee.objects.values().</p>
  <p>This is what I would need but with all the employees at once:</p>
  <pre><code>{
      &quot;message&quot;: &quot;success&quot;,
      &quot;data&quot;: {
          &quot;name&quot;: &quot;Fred&quot;,
          &quot;lastname&quot;: &quot;Wilson&quot;,
          &quot;salary&quot;: 28000,
          &quot;position&quot;: &quot;CTO&quot;,
          &quot;languages&quot;: [
              &quot;JavaScript&quot;,
              &quot;PHP&quot;
          ]
      }
  }
  </code></pre>
  <p>I've tried a lot of things including prefetch but I couldn't get it to work.</p>
  <p>EDIT:
  I came up with this one:</p>
  <pre><code>            data = []
              for queryset in Employee.objects.all():
                  myDict = {
                      'name': queryset.name,
                      'lastname': queryset.lastname,
                      'salary': queryset.salary,
                      'position': queryset.position.name,
                      'languages': [x['name'] for x in queryset.languages.all().values()],
                  }
                  data.append(myDict)
  return JsonResponse({&quot;message&quot;: &quot;success&quot;, &quot;data&quot;: data})
       
  </code></pre>
  <p>Is it inefficient and you would not use one of this to query the database ?.</p>