简体   繁体   English

Django关系数据库模型

[英]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. 我正在使用jsTree和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. 对于具有树型关系,您可以很好地使用出色的django-mptt模块。 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 . 假设您是一个“根节点”节点,它下面可以有n个节点,所有这些使您的树成为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. 因此,您所需要做的就是取这棵树A的根,并仅将特定的('root')作为外键。 So now, you have a record which has root of tree A as its foreign key. 因此,现在,您有了一个以树A的根作为外键的记录。

Next, to retrieve all the nodes in that tree, just call get_descendants() method on that root instance which you just made foreign key. 接下来,要检索该树中的所有节点,只需在刚刚创建了外键的根实例上调用get_descendants()方法。 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. 接下来,进入客户端部分,您将必须在要遍历此树的模板顶部立即加载mptt_tags 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. 现在,有一个漂亮的递归树templatetag,您可以使用它遍历模板中的树节点。 Also, refer to docs for how to make your model have tree kinda relationship by extending MPTTModel. 另外,请参考文档,以了解如何通过扩展MPTTModel使模型具有树种关系。

Apologies, if my example is not really great one, I am not really good at giving examples :D and hope it helps. 不好意思,如果我的例子不是很好,我不是很擅长举例:D,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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