【发布时间】:2012-01-18 11:35:04
【问题描述】:
我一直在绝望地尝试让它发挥作用。
我有一个模型,其中包含一个嵌入式对象的 ListField,基本上它是拍卖中的一个项目,其中包含一个出价列表。典型的 MongoDB 方法。
我知道 ListField 不会显示在管理员中,因为它不知道要显示什么小部件,它可能是任何内容的列表。这是有道理的。
我在我的 app 文件夹中创建了一个 fields.py 并将 ListField 子类化,我现在在我的 models.py 中使用它
我的问题是:
- 从现在开始,我如何继续进行,直到在我的管理页面上的项目部分下获得一个小部件,我可以在其中为所选项目添加出价?
这是我的model.py
from django.db import models
from djangotoolbox.fields import ListField
from djangotoolbox.fields import EmbeddedModelField
from ebay_clone1.fields import BidsListField
class User(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(max_length=75)
def __unicode__(self):
return self.name
class Item(models.Model):
seller = models.ForeignKey(User, null=True, blank=True)
title = models.CharField(max_length=100)
text = models.TextField()
price = models.FloatField()
dated = models.DateTimeField(auto_now=True)
num_bids = models.IntegerField()
bids = BidsListField(EmbeddedModelField('Bid'))
item_type = models.CharField(max_length=100)
def __unicode__(self):
return self.title
class Bid(models.Model):
date_time = models.DateTimeField(auto_now=True)
value = models.FloatField()
bidder = models.ForeignKey(User, null=True, blank=True)
在我的 fields.py 我有:
from django.db import models
from djangotoolbox.fields import ListField
from djangotoolbox.fields import EmbeddedModelField
from django import forms
class BidsListField(ListField):
def formfield(self, **kwargs):
return None
class BidListFormField(forms.Field):
def to_python(self, value):
if value in validators.EMPTY_VALUES:
return None
return value
def validate(self,value):
if value == '':
raise ValidationError('Empty Item String?')
【问题讨论】:
-
不应该已经工作了吗?代码看起来很完美。我在您的帖子中没有看到任何问题:-)
-
乔纳斯非常感谢您的帮助伙伴!看代码很好,但我知道我需要以某种方式子类 forms.Field 以便我可以告诉 Django 为我的嵌入式投标列表显示什么表单/小部件。
标签: django mongodb django-nonrel