如何从另一个Django模型中连接任何对象/模型? [英] How to agnostically link any object/Model from another Django Model?

查看:142
本文介绍了如何从另一个Django模型中连接任何对象/模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于Django的简单CMS。大多数内容管理系统依赖于在固定网址上使用具有一个或多个可编辑区域的模板的固定页面。要有一个可编辑区域,您需要一个页面。要使系统找出哪个页面,您需要该URL。



当您不再处理页面(是FlatPages页面或别的),而是另一个模型的实例。例如,如果我有一个产品型号,我可能希望创建一个详细页面,其中包含多个可编辑区域。



我可以建立这些区域进入模型,但在我的情况下,有几个模型,并且在我想要显示多少数据中有很多差异。



因此,我想建立CMS在模板级别,并指定一个块(可编辑区域)是基于页面的实例或其使用的模型。



我已经想要我可以在页面上转储自定义模板标签:

  {%block unique_objectunique placeholder name%} 

这将根据传入的两个参数找到一个块。一个例子: p>

 < h1> {{product_instance.name}}< / h1> 
{%block product_instancedetails:product short description%}
{%block product_instancedetail:product video%}
{%block product_instancedetail:product long description%}

听起来很厉害,对吧?那么我遇到的问题是如何为一个区域创建一个密钥,以便我可以将正确的块提取出来?我将处理一个完全未知的对象(它可以是一个页面对象,一个URL,一个模型实例,任何东西 - 甚至可以是一条船< / fg> )。



其他Django微型应用程序必须这样做。你可以用django标签来标记任何东西,对吧?我试图了解这是如何工作的,但我正在画空白。



所以,首先,我很生气吗?假设我不是,这看起来像是一个相对理智的想法,我应该如何去将一个对象+字符串链接到一个块/可编辑区域?



注意:编辑将完成页面,所以没有真正的问题让用户编辑区域。我不会在管理员中做任何反向的mumbo-jumbo。我的最终梦想是允许第三个参数来指定什么样的内容区域(文本,图像,视频等)。如果你有任何意见,我很高兴阅读他们!

解决方案

django-tagging使用Django的 contenttypes 框架。该文档比我可以做的更好的解释,但最简单的描述是可以指向任何其他模型的通用外键。



这可能是您正在寻找的,但从您的描述中,您也希望与其他现有项目非常相似:




  • django-flatblocks (...的行为像django.contrib.flatpages,但是部分页面;像一个可编辑的帮助框,你想要显示在主要内容。)


  • django-better-chunks (将它看作是可能需要插入模板并从管理界面进行管理的可重复使用内容的一小部分的平板。)




等等。如果这些是相似的,那么他们将为您做一个很好的起点。


I'm writing a simple CMS based on Django. Most content management systems rely on having a fixed page, on a fixed URL, using a template that has one or many editable regions. To have an editable region, you require a Page. For the system to work out which page, you require the URL.

The problem comes when you're no longer dealing with "pages" (be those FlatPages pages, or something else), but rather instances from another Model. For example if I have a Model of products, I may wish to create a detail page that has multiple editable regions within.

I could build those regions into the Model but in my case, there are several Models and is a lot of variance in how much data I want to show.

Therefore, I want to build the CMS at template level and specify what a block (an editable region) is based on the instance of "page" or the model it uses.

I've had the idea that perhaps I could dump custom template tags on the page like this:

{% block unique_object "unique placeholder name" %}

And that would find a "block" based on the two arguments passed in. An example:

<h1>{{ product_instance.name }}</h1>
{% block product_instance "detail: product short description" %}
{% block product_instance "detail: product video" %}
{% block product_instance "detail: product long description" %}

Sounds spiffy, right? Well the problem I'm running into is how do I create a "key" for a zone so I can pull the correct block out? I'll be dealing with a completely unknown object (it could be a "page" object, a URL, a model instance, anything - it could even be a boat</fg>).

Other Django micro-applications must do this. You can tag anything with django-tagging, right? I've tried to understand how that works but I'm drawing blanks.

So, firstly, am I mad? And assuming I not, and this looks like a relatively sane idea to persue, how should I go about linking an object+string to a block/editable-region?

Note: Editing will be done on-the-page so there's no real issue in letting the users edit the zones. I won't have to do any reverse-mumbo-jumbo in the admin. My eventual dream is to allow a third argument to specify what sort of content area this is (text, image, video, etc). If you have any comments on any of this, I'm happy to read them!

解决方案

django-tagging uses Django's contenttypes framework. The docs do a much better job of explaining it than I can, but the simplest description of it would be "generic foreign key that can point to any other model."

This may be what you are looking for, but from your description it also sounds like you want to do something very similar to some other existing projects:

  • django-flatblocks ("... acts like django.contrib.flatpages but for parts of a page; like an editable help box you want show alongside the main content.")

  • django-better-chunks ("Think of it as flatpages for small bits of reusable content you might want to insert into your templates and manage from the admin interface.")

and so on. If these are similar then they'll make a good starting point for you.

这篇关于如何从另一个Django模型中连接任何对象/模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆