Home > database >  query set too many objects to unpack expected(2) in django templateView
query set too many objects to unpack expected(2) in django templateView

Time:11-24

I have written a view to show open,completed,accepted and closed tickets on dashboard on clicking which goes into particular url to display the tickets accordingly and am switching the templates according to their status,I am quering the ticket status and i get the following error too many objects to unpack (expected 2)

models.py

class Ticket(models.Model):
    ticket_title = models.CharField(max_length=200)
    ticket_description = models.TextField()
    created_by = models.ForeignKey(User,related_name = 'created_by',blank=True,null=True,on_delete=models.CASCADE)

    STATUS_CHOICES = (
        ('Opened','Opened'),
        ('Accepted','Accepted'),
        ('Completed','Completed'),
        ('Closed','Closed')
    )

    status = models.CharField('Status',choices=STATUS_CHOICES,max_length = 100,default = 'Opened')

    closed_date = models.DateTimeField(blank=True,null=True)
    completed_date = models.DateTimeField(blank=True,null=True)
    accepted_date = models.DateTimeField(blank=True,null=True)
    opened_date = models.DateTimeField(blank=True,null=True)
    

    accepted_by = models.ForeignKey(User,related_name='assigned_to',on_delete=models.CASCADE,blank=True,null=True)

    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)


    def __str__(self):
        return self.ticket_title

Views.py

class DeveloperTicketView(TemplateView):
  

def get_template_names(self):
    ticket_type = Ticket.objects.filter('status')
    if ticket_type == "Opened":
        template_name = 'app/open_tickets.html'
    elif ticket_type == 'Accepted':
        template_name = 'app/dev_accepted_ticket.html'
    elif ticket_type == 'Completed':
        template_name = 'app/dev_completed_tickets.html'
    else:
        template_name = 'app/dev_closed_tickets.html'
    return template_name


def get_context_data(self, **kwargs):
    context =  super(TemplateView,self).get_context_data(**kwargs)
    context['open_tickets'] = Ticket.objects.filter(status = 'Opened')
    context['accepted_tickets'] = Ticket.objects.filter(status = 'Accepted',accepted_by = self.request.user)
    context['completed_tickets'] = Ticket.objects.filter(status = 'Completed',accepted_by = self.request.user)
    context['closed_tickets'] = Ticket.objects.filter(status = 'Closed',accepted_by = self.request.user)
    return context

CodePudding user response:

Error is due to this query...

Ticket.objects.filter('status')

You should specify something like:

if Ticket.objects.filter(status='Opened'):
    template_name = 'app/open_tickets.html')

Also please check the logic. Above query makes no sense in get_template_name... You should be getting one template according to request. So if request is for 'opened' then you should get template only for 'opened'.

Suppose if you want to get for 'opened' you need to pass it in query and do this:

if self.request.GET.get('status', '') == 'Opened':
    template_name = 'app/open_tickets.html')

CodePudding user response:

def show_ticket(request,status):
    if status.title() in ['Opened','Accepted','Completed','Closed']:

        if status.title() == 'Opened':
            tickets = Ticket.objects.filter(status='Opened')
        else:
            tickets = Ticket.objects.filter(status=status.title(),accepted_by=request.user)
        templates = {'Opened':'app/open_tickets.html','Accepted':'app/dev_accepted_ticket.html','Completed':'app/dev_completed_tickets.html','Closed':'app/dev_closed_tickets.html'}
        return render(request,templates[status.title()],{'tickets':tickets})

inside your urls.py

path('tickets/<str:status>/',views.show_ticket,name='show_ticket')

  • Related