Google App Engine ListField 中的 Django-nonrel
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的导入相互冲突:
第二次导入将用 djangotoolbox 的空模型模块替换 django.db 模型。在 Python 中使用
from X import *
通常是一个糟糕的主意,并且会产生像这样令人困惑的结果。如果您希望使用 djangotoolbox 中的 ListField,请使用:
并将 ListField 类引用为
fields.ListField
。Your imports are smashing each other:
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:
and refer to the ListField class as
fields.ListField
.好的,这就是我为了能够使用 ListFields 所做的事情。
MyClass
相当于您的Example
类,AnotherClass
与您的Choice_examples
相同。我所描述的内容将允许您在管理界面和您自己实现的视图中使用ListField
。我将从头开始
这就是我的模型的样子,
我希望能够使用管理界面使用列表字段的多选小部件来创建/编辑该模型的实例。因此,我创建了一些自定义类,如下所示
这些类允许在管理中使用列表字段。然后我创建了一个在管理站点中使用的表单
完成此操作后,我创建了一个管理模型并将其注册到管理站点
这现在正在我的代码中工作。请记住,这种方法可能根本不适合 google_appengine,因为我不太熟悉它的工作原理,并且可能会产生低效的查询等。
OK, here is what I did to be able to use ListFields.
MyClass
the equivalent to yourExample
class andAnotherClass
is the same as yourChoice_examples
. What I describe will allow you to useListField
s in the admin interface and your self implemented views.I'll start from the beginning
This is what what my model looks like
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
These classes allow the listfield to be used in the admin. Then I created a form to use in the admin site
After having done this I created a admin model and registered it with the admin site
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.
我不知道,但尝试一下:
I don't know, but try with:
看起来答案是您不能将对象传递到 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