Django:交叉引用

2024-09-30 03:25:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我的网站结构如下:

- Windows
    - 98
         - Subpage1
         - Subpage2
    - XP
         - ....
    - 7
    - ....
- Mac
    - 10.7
         - Subpage1
         - Subpage2
    - 10.8
         - ...
    - 10.9
- Ubuntu
    - 13.10

每个操作系统都是一个应用程序,我希望每个应用程序有一个额外的模型,代表一个交叉引用。引用可以转到任何页面。在

所以伪代码应该是这样的:

^{pr2}$

我找到了一个关于一般关系的帖子,但我不能使用我的问题的解决方案。在

链接到一个好的,易于理解的教程或任何帮助将是伟大的。在

谢谢

编辑:

我想这可能有用。但在本例中,我只能访问一个属性(slug),而不是整个对象:

from django.db import models
from itertools import chain

from otherapp.models import Geschaeftsfelder
from otherapp.models import Themenschwerpunkte
from otherapp.models import Themen

from django.utils.translation import ugettext as _
from common.fields import MarkdownTextField, translated_field

class Box(models.Model):

    title = models.CharField(max_length=100)

    headline_de = MarkdownTextField(verbose_name=_(u'Inhaltstext (dt.)'), blank=True)


    geschaeftsfelder = Geschaeftsfelder.objects.values_list('slug', 'title')
    themenschwerpunkte = Themenschwerpunkte.objects.values_list('slug', 'title')
    themen = Themen.objects.values_list('slug', 'title')

    result_list = chain(geschaeftsfelder, themenschwerpunkte, themen)

    links_to      = models.CharField(
                        max_length=200,
                        choices= result_list
                        )

编辑2: 我还尝试了一种通用内容类型的解决方案:

当我把下面的代码添加到我的Box模型中时,我会得到一个包含正确内容类型的下拉列表(themenschwerpunkte,geschaeftsfelder,…)。在

但我仍然无法创建到特定页面的链接。在

# http://stackoverflow.com/questions/6335986/how-can-i-restrict-djangos-genericforeignkey-to-a-list-of-models?lq=1
limit = models.Q(app_label = 'geschaeftsfelder', model = 'geschaeftsfelder') | models.Q(app_label = 'geschaeftsfelder', model = 'themenschwerpunkte') | models.Q(app_label = 'geschaeftsfelder', model = 'themen')
content_type = models.ForeignKey(ContentType, limit_choices_to = limit)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')

编辑3:

非常感谢您的回答和您投入的时间,但我将使用feinCMS而不是djangoCMS。这对我来说更容易理解,也更合适。在


Tags: tofromimport编辑objectstitlemodelslist
1条回答
网友
1楼 · 发布于 2024-09-30 03:25:19

内容类型方法似乎是合适的。django docs on generic relations是一个很好的来源。在

我复制了一些我正在使用的代码片段,也许它们很有用:

object_id    = models.PositiveIntegerField(blank=True, null=True)
content_type = models.ForeignKey(ContentType, blank=True, null=True, verbose_name="Verweist auf")
of           = generic.GenericForeignKey('content_type', 'object_id' )

admin中,我通过定义专用函数来使用内容类型,例如,一个用于获取所有空关系的函数(在您的例子中:没有选择操作系统)

^{pr2}$

相关问题 更多 >

    热门问题