【发布时间】:2013-11-11 08:31:19
【问题描述】:
我的网站结构如下:
- Windows
- 98
- Subpage1
- Subpage2
- XP
- ....
- 7
- ....
- Mac
- 10.7
- Subpage1
- Subpage2
- 10.8
- ...
- 10.9
- Ubuntu
- 13.10
每个操作系统都是一个应用程序,我希望每个应用程序都有一个额外的模型来表示交叉引用。引用可以转到任何页面。
所以伪代码看起来像这样:
class Reference(models.Model):
title = models.CharField(_('title'), max_length=200)
links_to = models.ForeignKey(Windows | Mac | Ubuntu)
我找到了一篇关于泛型关系的视图帖子,但我无法使用解决方案来解决我的问题。
一个好的、易于理解的教程或任何帮助的链接会很棒。
谢谢
编辑:
我认为这可能会奏效。但是在这个例子中我只能访问一个属性(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。这对我来说更容易理解,也更合适。
【问题讨论】:
-
当你说每个操作系统都是一个独立的app时,大概你的意思是它们是不同应用程序中的单独模型。这似乎是一个奇怪的数据模型:似乎最好有一个模型来保存 OS 的不同值。
-
这个结构和我的例子有点不同。每个操作系统都有不同的结构。
-
也许我不明白你的回答。能详细解释一下或者举个例子吗?
标签: python django django-cms