Google App Engine ListField 中的 Django-nonrel

发布于 2024-09-27 05:54:52 字数 859 浏览 9 评论 0原文

我正在尝试使用 django-nonrel 在 Google App Engine 中构建一个示例应用程序。并且在将 ListField 属性实现到模型中时遇到问题。

我创建了一个应用test_model,并将其作为已安装的应用包含在我的设置中。 model.py 是:

从 django.db 导入模型
从 djangotoolbox 导入 *
从 dbindexer 导入 *

# 在这里创建你的模型。
类示例(模型.模型):
    some_choices = models.ListField('Choice_examples')
    注释 = models.CharField(max_length='20')
    update_at = models.DateTimeField(auto_now=True)

    def __unicode__(自身):
        返回 u'%s' % (self.notes)


类 Choice_examples(models.Model):
    名称 = models.CharField(max_length='30')

    def __unicode__(自身):
        返回 u'%s' % (self.name)

上面的例子给了我:
AttributeError:'module'对象没有属性'Model'

如果我注释掉 djangotoolbox 导入,我会得到以下信息:
AttributeError:“模块”对象没有属性“ListField”

我在这里做错了什么?我似乎找不到任何关于如何在 django-nonrel 中使用 ListField 的文档。这是因为它应该非常明显吗?

I am trying to build an example app in Google App Engine using django-nonrel. and am having problems implementing ListField attribute into a model.

I have created an app test_model and have included it as an installed app in my settings. The model.py is:

from django.db import models
from djangotoolbox import *
from dbindexer import *

# Create your models here.
class Example(models.Model):
    some_choices = models.ListField('Choice_examples')
    notes = models.CharField(max_length='20')
    updated_at = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return u'%s' % (self.notes)


class Choice_examples(models.Model):
    name = models.CharField(max_length='30')

    def __unicode__(self):
        return u'%s' % (self.name)

The above example gives me:
AttributeError:'module' object has no attribute 'Model'

If I comment out the djangotoolbox import, I get the following :
AttributeError: 'module' object has no attribute 'ListField'

What am I doing wrong here? I can't seem to find any documention as to how to go about using ListField in django-nonrel. Is that because it is supposed to really obvious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

追风人 2024-10-04 05:54:52

您的导入相互冲突:

from django.db import models
from djangotoolbox import *

第二次导入将用 djangotoolbox 的空模型模块替换 django.db 模型。在 Python 中使用 from X import * 通常是一个糟糕的主意,并且会产生像这样令人困惑的结果。

如果您希望使用 djangotoolbox 中的 ListField,请使用:

from djangotoolbox import fields

并将 ListField 类引用为 fields.ListField

Your imports are smashing each other:

from django.db import models
from djangotoolbox import *

The second import will replace the django.db models with djangotoolbox' empty models module. Using from X import * is a terrible idea in general in Python and produces confusing results like these.

If you're looking to use ListField from djangotoolbox, use:

from djangotoolbox import fields

and refer to the ListField class as fields.ListField.

花间憩 2024-10-04 05:54:52

好的,这就是我为了能够使用 ListFields 所做的事情。 MyClass 相当于您的 Example 类,AnotherClass 与您的 Choice_examples 相同。我所描述的内容将允许您在管理界面和您自己实现的视图中使用ListField

我将从头开始

这就是我的模型的样子,

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

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

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

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

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

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

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,因为我不太熟悉它的工作原理,并且可能会产生低效的查询等。

OK, here is what I did to be able to use ListFields. MyClass the equivalent to your Example class and AnotherClass is the same as your Choice_examples. What I describe will allow you to use ListFields in the admin interface and your self implemented views.

I'll start from the beginning

This is what what my model looks like

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)

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.

对你而言 2024-10-04 05:54:52

我不知道,但尝试一下:

class Choice_examples(models.Model):
    name = models.CharField(max_length='30')

    def __unicode__(self):
        return u'%s' % (self.name)

class Example(models.Model):
    some_choices = models.ListField(Choice_examples)
    notes = models.CharField(max_length='20')
    updated_at = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return u'%s' % (self.notes)

I don't know, but try with:

class Choice_examples(models.Model):
    name = models.CharField(max_length='30')

    def __unicode__(self):
        return u'%s' % (self.name)

class Example(models.Model):
    some_choices = models.ListField(Choice_examples)
    notes = models.CharField(max_length='20')
    updated_at = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return u'%s' % (self.notes)
当爱已成负担 2024-10-04 05:54:52

看起来答案是您不能将对象传递到 fields.ListField 中。

我已经放弃了使用 ListField 的尝试,因为文档有限,而且我的编码技能达不到我解决这个问题的水平。

如果其他人遇到类似的问题,您应该考虑创建一个新模型来映射多对多关系。如果管理视图很重要,您应该查看以下内容以显示与任何给定管理视图内联的 ManyToMany 表:

http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#s-working-with-many -对多模型

Looks like the answer is that you cannot pass an object into fields.ListField.

I have ditched trying to work with ListField as documentation is limited and my coding skills aren't at a level for me to work it out.

Anyone else coming across a similar problem, you should consider create a new model to map the ManyToMany relationships. And if the admin view is important, you should look into the following to display the ManyToMany table inline with any given admin view:

http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#s-working-with-many-to-many-models

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文