兩種不同的方法:https://docs.djangoproject.com/en/dev/topics/db/queries/Django的 - 下面基於此頁面上的例子反向關係
class Blog(models.Model):
name = models.CharField(max_length=100)
class Entry(models.Model):
blog = models.ForeignKey(Blog)
headline = models.CharField(max_length=255)
我想找到博客名稱hello world
所有條目。有兩種方法可以做到這一點。數據庫性能有什麼不同?他們中的一個是更好還是更受歡迎?
b = Blog.objets.get(name='hello world')
b.entry_set.all()
OR
Entry.objects.filter(blog__name='hello world')
Blog.objects.get(name ='hello world')可能會導致一個異常Blog.DoesNotExist – crazyzubr
對,謝謝你的提示。 – Medorator