Colleen
Colleen

Reputation: 25489

Is there a way to create dependent fields in Django models?

Is there a way to automatically fill fields based on dependencies? E.g. I have a model with fields answer_type and chart_type, where I want (e.g.) all radio button answer_types to automatically be pie chart_types, and all autofill questions to automatically be bar chart_type.

I don't seem to be able to call functions based on fields within the model, so I'm not sure if this is feasible.

Upvotes: 1

Views: 523

Answers (1)

U-DON
U-DON

Reputation: 2140

What in particular is preventing you from calling functions based on the model fields? You should be able to define functions within the model, as such:

class YourModel(models.Model):
    answer_type = models.CharField(max_length=20)
    ...

    def chart_type(self):
        if self.answer_type == 'radio':
            return 'pie'
        ...

Let me know any problems you run into with that.

Upvotes: 1

Related Questions