Pracuję nad projektem, który używa django-mptt, ale otrzymuję dziwne wyniki, gdy używam funkcji get_ancestors
. Oto przykład.
Mam stworzył prosty model, odziedziczoną MPTTModel:Nieprawidłowe wyniki, gdy używam funkcji `get_ancestors` z` 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
I tu jest funkcja, która działa w tym modelu:
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)
Spodziewałem się zobaczyć kolejne wartości na wyjściu:
[<Classifier: aaa>, <Classifier: aa>, <Classifier: a>, <Classifier: root>]
[<Classifier: bbb>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
Ale Insted widzę:
[<Classifier: aaa>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
[<Classifier: bbb>, <Classifier: bb>, <Classifier: b>, <Classifier: root>]
Tak jak widzisz, ta funkcja wypisuje poprawną listę przodków dla węzła bbb
, ale niepoprawnych przodków dla węzła aaa
. Czy możesz mi wyjaśnić, dlaczego tak się dzieje? Czy jest to błąd w django-mptt
, czy mój kod jest niepoprawny?
Z góry dziękuję.