带有 Mongodb 列表字段的 Django-Nonrel [英] Django-Nonrel with Mongodb listfield

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

问题描述

我正在尝试在 mongodb 上的 django-nonrel 中实现多字段关系.建议在:

I am trying to implement manytomany field relation in django-nonrel on mongodb. It was suggessted at to:

ListField 的 Django-nonrel 表单字段

按照接受的答案

models.py

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

我不确定以下内容的去向,它已经在 fields.py、widgets、py、models.py 中进行了测试

i am not sure where the following goes, it has been tested in fields.py, widgets,py, models.py

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

admin.py

class MyClassAdmin(admin.ModelAdmin):
    form = MyClassForm

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

admin.site.register(MyClass, MyClassAdmin)

以下错误不断弹出:

如果models.py中使用了中间自定义类代码

If the middle custom class code is used in models.py

name 'SelectMultiple' is not defined

如果自定义类代码从models.py中去掉:

If custom class code is taken off models.py:

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

推荐答案

你只需要通过它的声音导入SelectMultiple.您可以将代码放在这三个文件中的任何一个中,fields.py 会有意义.

You just need to import SelectMultiple by the sound of it. You can put the code in any of those three files, fields.py would make sense.

因为它很常见:

from django import forms

在您的文件顶部,您可能只想将下面的代码编辑为:

at the top of your file already, you probably just want to edit the code below to:

# you'll have to work out how to import the Mongo ListField for yourself :)
class ModelListField(ListField):
    def formfield(self, **kwargs):
    return FormListField(**kwargs)

class ListFieldWidget(forms.SelectMultiple):
    pass

class FormListField(forms.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

您可能还想尝试了解更多有关 python 工作原理、如何导入模块等的知识.

You probably also want to try and learn a bit more about how python works, how to import modules etc.

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

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