Opeyemi Odedeyi
Opeyemi Odedeyi

Reputation: 780

getting all the instances of a model into an oblect

I have a model called sickness, which I have used to create multiple instances like malaria, typhoid, and others.

model

class Sickness(models.Model):
    name = models.CharField(max_length=30, unique=True)

    def __str__(self):
        return self.name

please, how can I get this into a dictionary form like this where the name will be gotten into the dictionary.

duration_choices = {
    'malaria':'malaria',
    'typhod':'typhod',
    'cough':'cough',
    'headache':'headache',
    'stomach gas':'stomach gas',
}

so, assuming I have created numerous sicknesses in my db like this:

enter image description here

that will be contained in the dictionary, and if others are added later, they will be contained in the dictionary when it is called for

Upvotes: 0

Views: 48

Answers (1)

asciialex
asciialex

Reputation: 133

I am not sure why you want the keys and values in the dictionary to be the same value, but you could achieve what you want with:

duration_choices = {i.name: i.name for i in Sickness.objects.all()}

Upvotes: 2

Related Questions