help with listview Django

Asked

Viewed 175 times

0

I need help picking up the values of an object in a list view

class Compraslist(LoginRequiredMixin, ListView):
model = Solicitacao
template_name = 'compraslist.html'

def get_context_data(self, **kwargs):
    context = super(Compraslist, self).get_context_data(**kwargs)
    context['solicitacoes'] = Solicitacao.objects.all()
    return context

this is my list class, it lists the requests that have been made what I need is that it only lists the requests that have the Free status = 1

class Orcamento(models.Model):
anexo_descricao = models.CharField("Descrição do Arquivo" ,max_length=100)
anexo_orcamento = models.FileField("Anexar Arquivo", upload_to='media/uploads/anexos/')
anexo_relacionamento = models.ForeignKey(Solicitacao, on_delete=models.CASCADE)
ROLE_CHOICES = (
    (1, 'Sim'),
    (2, 'Não')
)
liberacao_compras = models.PositiveSmallIntegerField("Aprovar Solicitação?",
    choices=ROLE_CHOICES,
    default = 1,
)

def __str__(self):
    return self.anexo_descricao

class Meta:
    verbose_name = 'Orcamento'
    verbose_name_plural = 'Orcamentos'

I tried to take it by the get_context_data as follows

class Compraslist(LoginRequiredMixin, ListView):
model = Solicitacao
template_name = 'compraslist.html'

def get_context_data(self, **kwargs):
    context = super(Compraslist, self).get_context_data(**kwargs)
    context['solicitacoes'] = Solicitacao.objects.all()
    context['anexo'] = Orcamento.objects.filter(anexoRelacionamento=self.kwargs['pk'])
    return context

but an error has been returned to me

   context['anexo'] = Orcamento.objects.filter(anexoRelacionamento=self.kwargs['pk'])

Keyerror: 'pk'

  • 1

    Your filter should be in the request model. Solicitation.objects.filter(clears_purchases=1) The pk error is something else. You cannot find the key in the arguments coming from the context: tries: Organizing.objects.filter(annex_relationship=kwargs['id']) the attributes must be lowercase.

  • that self.kwargs['pk'] refers to the model Solicitacao or Orcamento? what is the url of this view?

  • This pk Keyerror is because you are calling a parameter that has not been passed in your url. E reads in the Listview documentation, it is correct that you change the query in get_queryset and not in get_context_data.

  • The names aren’t right either. You have a view called Compraslist that uses a model called Request and the pk parameter you passed calls a Budget model. Think about whether it’s worth changing the name from Compraslist to Solicitacaodecompralist or Solicitacoeslist or something you prefer. And if you need to do a filter in Orcing with pk, change the url parameter to orcamento_pk because normally in urls when we use 'pk', this parameter is used in some filter of the main view model, which in this case is Request.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.