Как получить доступ к значениям атрибутов модели Foreign Key в другой модели в Django?
Я пытаюсь отфильтровать варианты атрибута models на основе его внешнего ключа атрибута models.
parent_choices = [('fruits', 'fruits'), ('veggie', 'veggie')]
dict_options = {
'fruits': [('apple', 'apple'), ('orange', 'orange')],
'veggie': [('carrot', 'carrot'), ('peas', 'peas')],
}
class Parent(models.Model):
name = models.CharField(max_length=50)
like_to_eat = models.CharField(max_length=20, choices=parent_choices)
class Child(models.Model):
name = models.CharField(max_length=50)
parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
given_to_eat = models.CharField(max_length=20, choices=dict_options[parent.like_to_eat])
Сообщение об ошибке:
AttributeError: 'ForeignKey' object has no attribute 'like_to_eat'