Django-nonrel 在管理中使用包含 EmbeddedObjects 的 ListField

发布于 2024-12-27 12:54:06 字数 1884 浏览 3 评论 0原文

我一直在绝望地尝试让它发挥作用。

我有一个模型,其中包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别低头,皇冠会掉 2025-01-03 12:54:06

试试这个?

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

Try this?

class BidsListField(ListField):
    def formfield(self, **kwargs):
        return BidListFormField(**kwargs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文