Yes, django-mptt支持嵌套模型。

huangapple go评论63阅读模式
英文:

Does django-mptt support nested models?

问题

以下是您提供的内容的中文翻译:

我有一个包含嵌套实体的数据库,实际上是关于部分的:街道-房屋-技术房间。换句话说,我想要在每个级别上具有不同属性的节点树。

我想要实现的是在这个数据库上使用django-mptt的功能,例如,我希望在Django Admin中将数据库视为树。

我在我的models.py中尝试了以下代码:

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

class Street(MPTTModel):
    name = models.CharField(max_length=100)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='streets')

    def __str__(self):
        return self.name

class House(MPTTModel):
    name = models.CharField(max_length=100)
    parent = TreeForeignKey(Street, on_delete=models.CASCADE, null=True, blank=True, related_name='houses')

    def __str__(self):
        return self.name

class TechRoom(MPTTModel):
    name = models.CharField(max_length=100)
    parent = TreeForeignKey(House, on_delete=models.CASCADE, null=True, blank=True, related_name='tech_rooms')

    def __str__(self):
        return self.name

以及我的admin.py如下:

from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from places.models import Street, House, TechRoom

admin.site.register(Street, MPTTModelAdmin)
admin.site.register(House, MPTTModelAdmin)
admin.site.register(TechRoom, MPTTModelAdmin)

问题是它像常规外键一样工作,而不是像树一样。是否可能使用django-mptt实现嵌套模型?更具体地说,是否可能在这里使用模型名称(例如Street)而不是'self':

parent = TreeForeignKey(Street, on_delete=models.CASCADE, null=True, blank=True, related_name='houses')
英文:

I have a database of nested entities, that actually address parts: Street-House-Technical Room. In other words, I'd like to have a tree of nodes with different attributes on each level.

What I'd like to implement is to use django-mptt features for this database, for example, I want to see the database as a tree in Django Admin.

I tried the following in my models.py:

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey


class Street(MPTTModel):
    name = models.CharField(max_length=100)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='streets')

    def __str__(self):
        return self.name


class House(MPTTModel):
    name = models.CharField(max_length=100)
    parent = TreeForeignKey(Street, on_delete=models.CASCADE, null=True, blank=True, related_name='houses')

    def __str__(self):
        return self.name


class TechRoom(MPTTModel):
    name = models.CharField(max_length=100)
    parent = TreeForeignKey(House, on_delete=models.CASCADE, null=True, blank=True, related_name='tech_rooms')

    def __str__(self):
        return self.name

and my admin.py looks as:

from django.contrib import admin
from mptt.admin import MPTTModelAdmin

from places.models import Street, House, TechRoom

admin.site.register(Street, MPTTModelAdmin)
admin.site.register(House, MPTTModelAdmin)
admin.site.register(TechRoom, MPTTModelAdmin)

The problem is it works as a regular Foreign key not as a tree.
Is it possible to implement nested models using django-mptt?
More specifically is it possible to use a model name (e.g. Street) instead of 'self' here:

parent = TreeForeignKey(Street, on_delete=models.CASCADE, null=True, blank=True, related_name='houses')

答案1

得分: 0

我找到了所需的解决方案。它是django-polymorphic-tree库。
作者说:

> 您可以编写Django模型,形成树形结构,其中每个节点
> 可以是不同的模型类型。

它有效果。

英文:

I found the required solution. It is the django-polymorphic-tree library.
The author says:

> You can write Django models that form a tree structure where each node
> can be a different model type.

And it works.

huangapple
  • 本文由 发表于 2023年5月20日 22:09:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定