#Annotate to DB field name

12 messages · Page 1 of 1 (latest)

cold ferry
#

Hello folks,
Is it possible to return object instead of id on foreign relation with annotate? eg:

class Times(models.Model)
  employee = models.ForeignKey(Employee,...

class Viewset()

def get_queryset(self):
        return (
            super()
            .get_queryset()
            .select_related("employee__user")
            .annotate(employee_full_name=Concat("employee__user__first_name", Value(" "), "employee__user__last_name", output_field=CharField()))
        ).order_by("-start_stamp")


what i need:
employee:{
"id",
"full_name":"",
}
astral swift
#

Yes, I think JSONObject will help you, if I understand correctly

cold ferry
astral swift
#

Yes, so you would need to name it something else.

cold ferry
astral swift
#

so annotate full_name onto the employee?

cold ferry
#

yes

#

like nested serializer

#

which gives me data about employee instead of its id

astral swift
#

I wonder if annotate(employee__full_name=Concat("employee__user__first_name", Value(" "), "employee__user__last_name", output_field=CharField())) would work? (note the __ in the definition of the variable)

cold ferry
#

ill try it out

cold ferry