ListField 的 Django-nonrel 表单字段 [英] Django-nonrel form field for ListField

查看:33
本文介绍了ListField 的 Django-nonrel 表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 appengine 上试验 django-nonrel 并尝试使用 djangotoolbox.fields.ListField 来实现多对多关系.正如我在文档中所读到的,ListField 是您可以用来解决不支持多对多关系的 djamgo-nonrel 的解决方法.

I'm experimenting with django-nonrel on appengine and trying to use a djangotoolbox.fields.ListField to implement a many-to-many relation. As I read in the documentation a ListField is something that you can use to make a workaround for djamgo-nonrel not supporting many-to-many relations.

这是我模型的摘录:

class MyClass(models.Model):
    field = ListField(models.ForeignKey(AnotherClass))

因此,如果我做对了,我将创建另一个类的外键列表,以显示与另一个类的多个实例的关系

So if I am getting this right I am creating a list of foreign keys to another class to show a relationship with multiple instances of another class

使用这种方法一切正常......没有例外.我可以在代码和视图中创建MyClass"对象.但是当我尝试使用管理界面时出现以下错误

With this approach everything works fine ... No Exceptions. I can create `MyClass' objects in code and views. But when I try to use the admin interface I get the following error

No form field implemented for <class 'djangotoolbox.fields.ListField'>

所以我想尝试一些我以前没有做过的事情.创建我自己的领域.实际上我自己的表单用于在管理界面中编辑 MyClass 实例.这是我所做的:

So I though I would try something that I haven't done before. Create my own field. Well actually my own form for editing MyClass instances in the admin interface. Here is what I did:

class MyClassForm(ModelForm):
    field = fields.MultipleChoiceField(choices=AnotherClass.objects.all(), widget=FilteredSelectMultiple("verbose_name", is_stacked=False))
    class Meta:
        model = MyClass

然后我将 MyClassForm 作为表单传递给管理界面

then I pass MyClassForm as the form to use to the admin interface

class MyClassAdmin(admin.ModelAdmin):
    form = MyClassForm

admin.site.register(MyClass, MyClassAdmin)

我认为这会起作用,但它不起作用.当我进入管理界面时,我收到与以前相同的错误.任何人都可以告诉我在这里做错了什么......或者如果您有使用 djangotoolbox 中的 ListFieldSetField 等的任何其他建议或成功案例.fields 在管理界面中,非常感谢.

I though that this would work but It doesn't. When I go to the admin interface I get the same error as before. Can anyone tell what I am doing wrong here ... or if you have any other suggestions or success stories of using the ListField, SetField, etc. from djangotoolbox.fields in the admin interface it would be very much appreciated.

推荐答案

好的,这是我为使这一切正常工作所做的...我从头开始

OK, here is what I did to get this all working ... I'll start from the beginning

这就是我的模型的样子

class MyClass(models.Model):
    field = ListField(models.ForeignKey(AnotherClass))

我希望能够使用管理界面使用列表字段的多选小部件来创建/编辑此模型的实例.因此,我创建了一些自定义类,如下

I wanted to be able to use the admin interface to create/edit instances of this model using a multiple select widget for the list field. Therefore, I created some custom classes as follows

class ModelListField(ListField):
    def formfield(self, **kwargs):
        return FormListField(**kwargs)

class ListFieldWidget(SelectMultiple):
    pass

class FormListField(MultipleChoiceField):
    """
    This is a custom form field that can display a ModelListField as a Multiple Select GUI element.
    """
    widget = ListFieldWidget

    def clean(self, value):
        #TODO: clean your data in whatever way is correct in your case and return cleaned data instead of just the value
        return value

这些类允许在管理员中使用列表字段.然后我创建了一个表单以在管理站点中使用

These classes allow the listfield to be used in the admin. Then I created a form to use in the admin site

class MyClassForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyClasstForm,self).__init__(*args, **kwargs)
        self.fields['field'].widget.choices = [(i.pk, i) for i in AnotherClass.objects.all()]
        if self.instance.pk:
            self.fields['field'].initial = self.instance.field

    class Meta:
        model = MyClass

完成此操作后,我创建了一个管理模型并将其注册到管理站点

After having done this I created a admin model and registered it with the admin site

class MyClassAdmin(admin.ModelAdmin):
    form = MyClassForm

    def __init__(self, model, admin_site):
        super(MyClassAdmin,self).__init__(model, admin_site)

admin.site.register(MyClass, MyClassAdmin)

这现在在我的代码中工作.请记住,这种方法可能根本不适合 google_appengine,因为我不太熟悉它的工作原理,而且它可能会创建低效的查询.

This is now working in my code. Keep in mind that this approach might not at all be well suited for google_appengine as I am not very adept at how it works and it might create inefficient queries an such.

这篇关于ListField 的 Django-nonrel 表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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