How to get all choices in a list

Submitted 4 years, 7 months ago
Ticket #174
Views 389
Language/Framework Django
Priority Medium
Status Closed

Suppose there are 7 choices ,e.g 7 days

I want all the name of 7 days in a list

I do not want to chhose a single one

Submitted on Sep 21, 20

like you want to have the data like this ['Monday','Tuesday','Wednesday'] .. is that correct? - sunil 4 years, 7 months ago
add a comment

2 Answers

def weekdays(weekday):
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    index = days.index(weekday)
    return (days + days)[index:index+7]


day_list = weekdays("Sunday")
print(day_list)

Is this what you are expecting ??

Submitted 4 years, 7 months ago


Verified

I think you can use model choice field name like this,

Let say you have model like below,

class Test(models.Model):
    APPROVED_CHOICE = [('Y', 'Approved'), ('N', 'Pending'), ]
    approved = models.CharField(
        max_length=1, choices=APPROVED_CHOICE, default='Y')
    slug = models.SlugField(max_length=2000, null=True, blank=True)

Now you can retrieve all the corresponding APPROVE_CHOICE field like below,

#Model.field name
print(Test.APPROVED_CHOICE)

Submitted 4 years, 7 months ago

@Aruntck i want result like ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] all these days are in choice field

- ashutoshmishra333 4 years, 7 months ago

t=Test() new=t.APPROVED_CHOICE for i in new: print(i[1]) task completed

- ashutoshmishra333 4 years, 7 months ago

Excellent

- Vengat 4 years, 7 months ago

If everything looks good. Then May I ask you to close the ticket.

- Vengat 4 years, 7 months ago

Can you close the ticket using techions answer?. Otherwise as per policy we will choose and close the ticket.

- Vengat 4 years, 7 months ago


Latest Blogs