2012-09-29 12 views
5

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ę.

Odpowiedz

4

Po wstawieniu węzła do drzewa powoduje on zmiany w całym drzewie. Tak więc po wstawieniu węzła b, twoje węzły a i root zmieniają się w bazie danych, ale twoje zmienne nie są aktualizowane i pozostają w nich stare, lewe/prawe wartości, które są używane do budowania poprawnej struktury drzewa.

W przypadku, gdy linia aa.insert_at(a, save = True) jest w proccess, zmienna a zawiera starą instancję lft = 2 i rght = 3, natomiast w bazie a węzeł zawiera lft = 4 i rght = 5.

Ty musisz wstawić nową instancję rodzica przed włożeniem nowego przedmiotu. Najprostszym sposobem wykonania tej czynności jest uruchomienie usługi: refresh_from_db:

aa.refresh_from_db() 
Powiązane problemy