0

My models:

class Question(models.Model):
    question_text = models.CharField(max_length=200)

class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)

I want to set new attribute 'choices' to Question model so it lists all Choices related to the question. It should be something like "choices = X.choice_set.all()?"

1 Answer 1

2

You can add

class Choice(models.Model):
    question = models.ForeignKey(Question, related_name='choices')

To have backward relation also. And, Yes you can add it attribute like Property, from django docs:

Also known as “managed attributes”, and a feature of Python since version 2.2. This is a neat way to implement attributes whose usage resembles attribute access, but whose implementation uses method calls.

@property
def choices:
 return self.choice_set.all()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.