डीजेगो क्वेरीसेट मॉडल उदाहरणों की सूचियों को वापस करना चाहते हैं, जो एक ही परिणाम तालिका में शामिल कई तालिकाओं से डेटा लौटने की एसक्यूएल धारणा के लिए काफी मानचित्र नहीं है।
SQL क्वेरी आप एक उदाहरण के रूप प्रदान की प्रभाव को प्राप्त करने के लिए, आप निम्न में से एक कर सकता है:
1) आप गीत से अपने डेटा पर पुनरावृति करना चाहते हैं, तो आप गीत आप क्वेरी सीमित कर सकते हैं कलाकार आईडी, इसलिए तरह से:
songs = Song.objects.filter(album__artist__id=123)
for song in songs:
print song.name
print song.album.name
... do what ever you need to do with song here ...
आप प्रदर्शन के बारे में चिंतित हैं, तो आप अपने क्वेरीसमूह को select_related() जोड़ सकते हैं।
# the depth argument tells django to only follow foreign key relationships
# from Song one level deep
songs = Song.objects.select_related(depth=1).filter(album__artist__id=123)
for song in songs:
# django has now already fetched the album data, so accessing it
# from the song instance does not cause an additional database hit
print song.album.name
2) आप एल्बम से अपने डेटा पर पुनरावृति करना चाहते हैं, तो आप ऐसा तरह कर सकते हैं:
albums = Album.objects.filter(artist__id = 123)
for album in albums:
for song in album.song_set.all():
print song.name
print album.name
... etc ...
मैं अपने डेटाबेस की सामग्री के लिए पूछ नहीं थी, लेकिन अपने मॉडल (या डेटाबेस स्कीमा) और एसक्यूएल आप प्रदर्शन करना चाहते हैं। – gruszczy
मैंने इसे फिर से संपादित किया :) –