Django not showing up all fields using objects with select_related

Hi! i’m facing an annoing bug in Django and i don’t know how i can solve it.

I’m trying to show up names from one other table using INNER JOIN but isn’t working.

Just to clarify, i have two database canaisAssoc and formatosAssoc, i’m trying to show up from canaisAssoc the name of the canais and from formatosAssoc the name of the formato, if you take a look at my, i’m using ajax, because i need to choose one value from one select in template to fill up others, the issue is, when i run the canaisAssoc.objects.select_related(‘idCanal’).filter(idSite_id = site, ativo = True).all()

if i run this


The return of the query is:

SELECT, campanhas_canaisassoc.idSite_id, campanhas_canaisassoc.idCanal_id, campanhas_canaisassoc.ativo,, campanhas_canais_dados.nomeCanais, campanhas_canais_dados.ativo FROM campanhas_canaisassoc INNER JOIN campanhas_canais_dados ON (campanhas_canaisassoc.idCanal_id = WHERE (campanhas_canaisassoc.ativo = True AND campanhas_canaisassoc.idSite_id = 3)

If i run this in phpMyAdmin, they are working as expected, showing up all values i’m need it


However, if i inspect the call of JSON, they are returning this:

{“channel”: [{“model”: “campanhas.canaisassoc”, “pk”: 14, “fields”: {“idSite”: 3, “idCanal”: 13, “ativo”: true}}, {“model”: “campanhas.canaisassoc”, “pk”: 15, “fields”: {“idSite”: 3, “idCanal”: 1, “ativo”: true}}, {“model”: “campanhas.canaisassoc”, “pk”: 16, “fields”: {“idSite”: 3, “idCanal”: 4, “ativo”: true}}, {“model”: “campanhas.canaisassoc”, “pk”: 17, “fields”: {“idSite”: 3, “idCanal”: 10, “ativo”: true}}, {“model”: “campanhas.canaisassoc”, “pk”: 63, “fields”: {“idSite”: 3, “idCanal”: 30, “ativo”: true}}]

Note in the fields there are only 3 values, idSite,idCanal and ativo, nothing i can do in the query returns me the nomeCanais, even using canaisAssoc.objects.raw with que query above doesn’t works, any ideas on how i can solve this? below are my codes!


class Canais_dados(models.Model):
  id = models.AutoField(primary_key=True)
  nomeCanais = models.TextField()
  ativo = models.BooleanField(default=True)
  def __str__(self):
    return self.nomeCanais

class Formatos_dados(models.Model):
  id = models.AutoField(primary_key=True)
  nomeFormato = models.TextField()
  ativo = models.BooleanField(default=True)
  def __str__(self):
    return self.nomeFormato    `Preformatted text`

class canaisAssoc(models.Model):
  id = models.AutoField(primary_key=True)
  idSite = models.ForeignKey(Sites_dados, on_delete=models.CASCADE, related_name='idSitesCanaisAssoc',null=False)
  idCanal = models.ForeignKey(Canais_dados, on_delete=models.CASCADE, related_name='idCanaisCanaisAssoc',null=False)
  ativo = models.BooleanField(default=True,null=True)  

class formatosAssoc(models.Model):
  id = models.AutoField(primary_key=True)
  idSite = models.ForeignKey(Sites_dados, on_delete=models.CASCADE, related_name='idSiteFormatoAssoc',null=False)
  idFormato = models.ForeignKey(Formatos_dados, on_delete=models.CASCADE, related_name='idFormatoFormatoAssoc',null=False)
  ativo = models.BooleanField(default=True,null=True)

def load_channel(request,site):
  channels = canaisAssoc.objects.select_related('idCanal').filter(idSite_id = site, ativo = True).all()
  formats = formatosAssoc.objects.select_related('idFormato').filter(idSite_id = site,ativo = True).all()
  channelName = serialize("json",channels)
  serializedChannel = json.loads(channelName)
  formatName = serialize("json",formats)
  serializedFormat = json.loads(formatName)
  return JsonResponse({'formats':serializedFormat,'channel': serializedChannel})

I’m sorry, I’m not sure I’m following from your description what you’re trying to do with your models.

What I think you’re looking for is that, given an instance of canaisAssoc you want to retrieve the nomeCanias field from the related Canais_dados.

If that’s correct, then if you have an instance of canaisAssoc named canais_assoc, then canais_assoc.idCanal.nomeCanais is the reference you’re looking for.

Hi Ken!

Sorry for my late reply! it works! many thanks on this answer, now i could understand better how ORM works! thank you again!