ForeignKey形式限制在Django [英] ForeignKey form restrictions in Django

查看:99
本文介绍了ForeignKey形式限制在Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django来编写一个博客应用程序,我正在尝试实现一个层次结构。每个类别都有一个父外键指向同一个类别模型。我想允许管理员添加类别,我希望界面允许他们选择一个类别的父类别。但是,我想避免我自己的祖父情况,所以我想把类别的可用选择限制在那些没有类别的祖先。

I'm using Django to write a blog app, and I'm trying to implement a hierarchical category structure. Each category has a "parent" ForeignKey pointing back to the same Category model. I want to allow admins to add categories, and I want the interface to allow them to select a category's parent category. However, I want to avoid I'm-my-own-grandpa situations, so I want to limit the available choices of categories to those which do not have category in question as an ancestor.

现在,我正在从这个视图中控制:

Right now, I'm controlling this from the view:

parent_candidates = list(Category.objects.all())
pruned_parent_list = [cat for cat in parent_candidates if instance.id not in cat.getHierarchy()]

其中实例是正在编辑的类别,getHierarchy()是获取祖代ids列表的方法。

where instance is the category being edited and getHierarchy() is a method to get a list of ancestor ids.

有一些问题这种方法。特别是,它使用额外的数据库命中来获取所有类别的列表,它使我通过循环遍历pruned_pa​​rent_list来将选择机制写入我的模板,以获取选项,当我真的只是指定一个小部件。

There are a number of problems with this approach. In particular, it uses an extra database hit to get the list of all categories and it makes me write the selection mechanism into my template by looping through pruned_parent_list to get the options, when I'd really rather just specify a widget.

有没有更好的方法来做到这一点?我知道我可以在后端添加自定义验证来防止这种情况,但为什么给用户提供选项?

Is there any better way to do this? I know I can add custom validation on the back-end to prevent this, but why give users the option?

推荐答案

如果我正确理解你的困境,问题本身就在于你处理哪些类别可以是父母的方式,哪些类别不能。避免这些问题的一个选择是实际限制可以成为父母的类别。例如,假设您有以下类别:

If I understand your predicament correctly, the problem itself lies with the way you're dealing with which categories can be parents and which ones can't. One option to avoid these problems is to actually limit the level of categories which can become parents. For example, let's say you have the following categories:


  • Internet


    • Google

    • 雅虎


    • MS Office

    • OpenOffice

    我通常处理这个的方式是我在category表上显然有一个parent_id FK。对于根元素(Internet,Offline),parent_id将为0.因此,在您的视图中,您正在尝试检索下拉菜单中的父类别,您需要确定它们能够保持嵌套的距离。我主要将其限制在第一级,所以要选择在您的下拉列表中显示哪些类别,您可以执行以下操作:

    The way I usually handle this is I obviously have a parent_id FK on the categories table. For the root elements (Internet, Offline), the parent_id would be 0. So, when in your view you're trying to retrieve the "parent categories" for the dropdown, you need to decide how far down can they keep nesting. I mostly limit this to the first level, so to choose which categories to show in your dropdown, you'd do something like:

    parents = Category.objects.filter(parent_id=0)
    

    ,但您可以增加您想要包含的级别,并在您的模板中制作出一些视觉识别系统,用于下拉菜单(包括层次结构中的每个级别的其他空格或破折号)。

    Now obviously, this limits the approach somewhat, but you can increase the level you'd like to include and work out some kind of visual identification system in your template for the dropdown (including extra spaces or dashes for each level in the hierarchy or something).

    无论如何,对于长期的回应抱歉,希望这样解决你的问题。

    Anyway, sorry about the long response, and hopefully this addressed your issue somewhat.

    这篇关于ForeignKey形式限制在Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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