Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django: Fields as list element in models

Actually I don’t know if its possible but this is a rough example of what do I want

its like,

class ImAModel(models.Model):
   field_of_list_of_questions = (models.BooleanField(), models.BooleanField(), models.BooleanField())

I need this beacuse I will have a model that have a vary field. And also there may be a lot of fields in it. Then I want to call them like this val = model.field_of_lists_of_questions[1]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’ll be very enjoying if it’s possible.

>Solution :

No, you can however define three fields, and a property to group these:

class ImAModel(models.Model):
    question1 = models.BooleanField()
    question2 = models.BooleanField()
    question3 = models.BooleanField()

    @property
    def list_of_questions(self):
        return (self.question1, self.question2, self.question3)

    @list_of_questions.setter
    def list_of_questions(self, value):
        self.question1, self.question2, self.question3 = value
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading