本文介绍了Django-nonrel在管理中使用包含EmbeddedObjects的ListField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在无奈地尝试使它生效.

I've been trying hopelessly to get this to work.

我有一个包含EmbeddedObjects的ListField的模型,基本上它是拍卖中的一个项目,其中包含一个投标列表.典型的MongoDB方法.

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.

我知道ListField不会显示在管理员中,因为它不知道要显示什么小部件,它可能是任何东西的列表.那是有道理的.

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.

我已经在我的应用程序文件夹中创建了一个fields.py,并将其子类化为ListField,现在我在models.py中使用了它.

I've created a fields.py in my app folder and subclassed ListField and I'm now using this in my models.py

我的问题是:

  • 从现在开始,如何继续操作,直到在管理页面的项目"部分下找到小部件,可以在其中将出价添加到所选的项目中?

这是我的模特.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中,我有:

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?')

推荐答案

尝试一下?

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

这篇关于Django-nonrel在管理中使用包含EmbeddedObjects的ListField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 14:52