[Fixed]-Django: Use other model classes as choices for a field

1👍

From what i can see, you’re not using the BaseRating model for anything, so it’s safe to remove it.

As for the questions, in my case, i’ll create two new models that have foreign keys to both FiveStarRating and YesNoRating so the data can be exclusive from each other. so you’ll end up having two models like this.

class FiveStarQuestion(models.Model):
    title = models.CharField(max_length=255)
    rating = fields.ForeignKey(FiveStarRating)

class YesNoQuestion(models.Model):
    title = models.CharField(max_length=255)
    rating = fields.ForeignKey(YesNoRating)

but if you want to share the titles among the two questions (I would second this approach because there might be two questions with the same title)

Example:
How would you rate Stackoverflow

and

How Satisfied are you with Stackoverflow

It makes sense to have only one title called Stackoverflow and use that Reference as a foreignkey in our tables. So in this case, you can keep the Question model and have ForiegnKey fields that point to it.

class Question(models.Model):
    title = models.CharField(max_length=255)

The create the two models as follows:

class FiveStarQuestion(models.Model):
    title = models.ForeignKey(Question)
    rating = fields.ForeignKey(FiveStarRating)

class YesNoQuestion(models.Model):
    title = models.ForeignKey(Question)
    rating = fields.ForeignKey(YesNoRating)

Leave a comment