से 'get_ancestors' फ़ंक्शंस का उपयोग करते समय गलत परिणाम मैं एक परियोजना विकसित कर रहा हूं जो django-mptt का उपयोग करता है, लेकिन जब मैं get_ancestors
फ़ंक्शन का उपयोग करता हूं तो मुझे अजीब परिणाम मिलते हैं। यहाँ एक उदाहरण है।
मैं एक एक साधारण मॉडल, MPTTModel से विरासत में मिली बनाया है:`django-mptt`
class Classifier(MPTTModel):
title = models.CharField(max_length=255)
parent = TreeForeignKey('self', null = True, blank = True,
related_name = 'children')
def __unicode__(self):
return self.title
और यहाँ समारोह जो इस मॉडल के साथ काम करता है:
def test_mptt(self):
# Erase all data from table
Classifier.objects.all().delete()
# Create a tree root
root, created = Classifier.objects.get_or_create(title=u'root', parent=None)
# Create 'a' and 'b' nodes whose parent is 'root'
a = Classifier(title = "a")
a.insert_at(root, save = True)
b = Classifier(title = "b")
b.insert_at(root, save = True)
# Create 'aa' and 'bb' nodes whose parents are
# 'a' and 'b' respectively
aa = Classifier(title = "aa")
aa.insert_at(a, save = True)
bb = Classifier(title = "bb")
bb.insert_at(b, save = True)
# Create two more nodes whose parents are 'aa' and 'bb' respectively
aaa = Classifier(title = "aaa")
aaa.insert_at(aa, save = True)
bba = Classifier(title = "bbb")
bba.insert_at(bb, save = True)
# Select from table just created nodes
first = Classifier.objects.get(title = "aaa")
second = Classifier.objects.get(title = "bbb")
# Print lists of selected nodes' ancestors:
print first.get_ancestors(ascending=True, include_self=True)
print second.get_ancestors(ascending=True, include_self=True)
मैं उत्पादन पर अगले मान देखने के लिए उम्मीद:
[<Classifier: aaa>, <Classifier: aa>, <Classifier: a>, <Classifier: root>]
[<Classifier: bbb>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
लेकिन insted मैं देख रहा हूँ:
[<Classifier: aaa>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
[<Classifier: bbb>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
तो जैसा कि आप देखते हैं कि यह फ़ंक्शन bbb
नोड के लिए पूर्वजों की सही सूची प्रिंट करता है, लेकिन aaa
नोड के लिए गलत पूर्वजों। क्या आप मुझे समझा सकते हैं ऐसा क्यों होता है? क्या यह django-mptt
में एक बग है या मेरा कोड गलत है?
अग्रिम धन्यवाद।