Pisa Ponente
Pisa Ponente

Reputation: 123

Create a list using values_list() in django

I'm trying to get the values of the objects saved in my database in a list and as integer, but the code I'm using is not working: if I try number[1:] I'm just gettin a blank variable in the html page and I keep getting errors telling me I'm still working with queryset and not list.

number = list(Week.objects.filter(user=request.user).values_list())

How Can I get a simple list using .values_list()?

EDIT: Added the model

class Week(models.Model):    
    user = models.ForeignKey(UserData, on_delete=models.CASCADE)
    file = models.OneToOneField(File, on_delete=models.CASCADE)
    monday     = models.PositiveIntegerField(null=True)
    tuesday    = models.PositiveIntegerField(null=True)
    wednesday  = models.PositiveIntegerField(null=True)
    thursday   = models.PositiveIntegerField(null=True)
    friday     = models.PositiveIntegerField(null=True)
    saturday   = models.PositiveIntegerField(null=True)
    sunday     = models.PositiveIntegerField(null=True)

Upvotes: 2

Views: 214

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477210

number = list(Week.objects.values_list(
    'monday', 'tuesday', 'wednesday', 'thursay', 'friday', 'saturday', 'sunday'
).get(user=request.user, file=some_file))

I think it is better to explicitly list the fields: if later additional fields will appear in the Week module, it eventually can result in a lot of trouble to ensure that these are not in the number list.

Upvotes: 1

Related Questions