简体   繁体   中英

Django Relational Database Models

I have what I hope is a simple question.

I am making an application that has a tree like layout using jsTree with django. Currently each of the nodes in the tree have their own object, but I need multiple trees so I am looking to relate all of the nodes for one tree to another object which will represent one whole tree with all the nodes in it.

Hopefully the above makes sense. From what I can tell I need a foreignkey relationship between the nodes and the entire tree object/model. Ideally someone could point out to me how to write the foreignkey relationship correctly, and then how I would instantiate a fulltree object on the client side potentially?

Thank you for any help and suggestions.

Updated code: Model

from django.db import models

class StoringJSON(models.Model):
    parent = models.CharField(null=True, max_length=50)
    id = models.CharField(primary_key=True, max_length=50, unique=True)
    text = models.CharField(null=True, max_length=50)
    tree = models.Foreignkey(FullTree)

class FullTree(models.Model):
    pass

Serializer

from rest_framework import serializers
from treetool.models import StoringJSON
from django.contrib.auth.models import User

class TreeSerializer(serializers.ModelSerializer):
    owner = serializers.ReadOnlyField(source='owner.username')

    class Meta:
        model = StoringJSON
        fields = ('id', 'text', 'parent')

View

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.db import models


@login_required
def tree(request):
    return render(request, 'treetool/tree.html')


from treetool.models import StoringJSON
from treetool.serializers import TreeSerializer

from rest_framework import generics


class TreeList(generics.ListCreateAPIView):
    queryset = StoringJSON.objects.all()
    serializer_class = TreeSerializer

class TreeDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = StoringJSON.objects.all()
    serializer_class = TreeSerializer

For having a tree kind of relationships, you can very well use excellent django-mptt module. What this package does is something like this.

Lets say you a node which is "root node", you can have n number of nodes below it and all this makes your tree A . Now lets say you want to have a record where you want all this nodes of tree to attached to. So all you have to do is take the root of this tree A and only make that particular('root') as the foreign key. So now, you have a record which has root of tree A as its foreign key.

Next, to retrieve all the nodes in that tree, just call get_descendants() method on that root instance which you just made foreign key. Voila, you have your whole tree.

Next, coming to your client side part, you will have to load mptt_tags right at the top of template in which you want to traverse this tree. You will have to pass all the nodes in an array where node at zero index is the root node of the tree. You can construct this array by using

nodes = root.get_descendants() nodes.insertAt(0, root)

Now, there is a beautiful recursetree templatetag that you can use to traverse the tree nodes in template. Also, refer to docs for how to make your model have tree kinda relationship by extending MPTTModel.

Apologies, if my example is not really great one, I am not really good at giving examples :D and hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM