Django-nonrel 在管理中使用包含 EmbeddedObjects 的 ListField
我一直在绝望地尝试让它发挥作用。
我有一个模型,其中包含 EmbeddedObjects 的 ListField,基本上它是拍卖中的一个项目,其中包含出价列表。典型的 MongoDB 方法。
我知道 ListField 不会显示在管理中,因为它不知道要显示什么小部件,它可能是任何内容的列表。这是有道理的。
我在我的应用程序文件夹中创建了一个 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?')
I've been trying hopelessly to get this to work.
I have a model that contains a ListField of EmbeddedObjects, basically it's an Item in an auction that contains a list of bids within it. Typycal MongoDB approach.
I understand that ListField doesn't show up in the admin since it doesn't know what Widget to display, it could be a list of anything. That makes sense.
I've created a fields.py in my app folder and subclassed ListField and I'm now using this in my models.py
My question is:
- How do I keep going from this point onwards until getting a widget on my admin page under the Item section where I can add bids to the Item selected?
Here's my 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)
In my fields.py I have:
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?')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个?
Try this?