Hi there.
supposed you've got a nested model like this:
class Article(models.Model):
parent_article = models.OneToOneField("self", blank=True, null=True, default=None, related_name="child_article")
How would you traverse the related model chain ( aka linked list ) to one end?
Maybe with a generator that makes you a nice list, that can be printed:
def article_child_gen(article):
yield article.child_article
while True:
yield from article_child_gen(article.child_article)
This throws an exception if it reaches the end. You catch it and return. Great. In theory. The problem is that its not easy to catch it, because you are not allowed to catch it.
It's name is RelatedObjectDoesNotExist, defined by SingleRelatedObjectDescriptor in django.db.models.fields.related . So my question is: is there a way to catch this anyway, or to get around it? Because the other path that i am going now is to Article.objects.filter(parent_article=blabla) me through this problem. But this just doesn't seem to be a clean solution.
thanks!