2012-01-18 22 views
3

我一直在試圖絕望地讓這個工作。Django-nonrel使用包含管理的嵌入對象的ListField

我有一個包含EmbeddedObjects的ListField的模型,基本上是在包含在它的出價清單拍賣的項目。 Typycal MongoDB方法。

我明白ListField不會在管理中顯示,因爲它不知道什麼widget中顯示,它可能是任何東西的清單。這就說得通了。

我在我的應用程序文件夾中創建一個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?') 
+1

不應在此工作了嗎?代碼看起來很完美。我的帖子中沒有看到任何問題:-) – 2012-01-19 18:23:13

+0

喬納斯非常感謝您的幫助!看看代碼是好的,但我知道我需要以某種方式將forms.Field子類化,以便我可以告訴Django爲我的嵌入式出價列表顯示何種表單/小部件。 – holografix 2012-01-20 00:24:48

回答

1

試試這個?

class BidsListField(ListField): 
    def formfield(self, **kwargs): 
     return BidListFormField(**kwargs)