I am still getting the hang of Django. For example, you have two models and one of them is related to the other using ForeignKey.
class Parent(models.Model):
name = models.CharField(max_length=255)
birthday= models.DateField(blank=True,null=True)
class Child(models.Model):
name = models.CharField(max_length=255)
parent= models.ForeignKey(Parent)
In the example above, i would like to access the particular child and get his name. I will like to do it through the parent instance. So for example, i have a Parent called John and i would like to know his child’s name. How do i do it?
Please pardon me if it is a simple question…
The below code addresses your question. Note that
child_setis the related manager’s default name. For more information, see https://docs.djangoproject.com/en/dev/ref/models/relations/