更改标签通用内联管理

发布于 2024-12-03 10:27:57 字数 1811 浏览 0 评论 0原文

我在 models.py 中执行以下操作:

class Item(models.Model):
    date = models.DateField(_('date'), blank=True, null=True)
    description = models.CharField(_('description'), max_length=255)

    content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
    object_id = models.PositiveIntegerField(_('object id'), db_index=True)
    object = generic.GenericForeignKey('content_type', 'object_id')

class ItemAccountAmountRef(Item):
    """ Items of which a Quote or an Invoice exists. """
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    reference = models.CharField(max_length=200)
    debit_account = models.ForeignKey(Account, related_name='receivables_receipt_debit_account')
    credit_account = models.ForeignKey(Account, related_name='receivables_receipt_credit_account')

class PaymentItem(ItemAccountAmountRef):
    pass

class Payment(models.Model):
    invoice = models.ManyToManyField(Invoice, null=True, blank=True)
    date = models.DateField('date')
    attachments = generic.GenericRelation(Attachment)
    site = models.ForeignKey(Site, related_name='payment_site', null=True, blank=True
    items = generic.GenericRelation(PaymentItem)

在 admin.py 中:

class PaymentItemInline(generic.GenericTabularInline):
    model = PaymentItem
    form = PaymentItemForm

class PaymentAdmin(admin.ModelAdmin):
    inlines = [PaymentItemInline]

在 forms.py 中:

class PaymentItemForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(PaymentItemForm, self).__init__(*args, **kwargs)
        self.fields['credit_account'].label = "Bank Account"

在 PaymentItemInline 中,标签没有更改。我尝试过更改其他属性,例如有效的类。如果我在调试模式下运行 init,我可以看到标签变量正在更改,但是当呈现表单时,该字段仍然标记为信用帐户。有什么建议吗?

I the following in the models.py:

class Item(models.Model):
    date = models.DateField(_('date'), blank=True, null=True)
    description = models.CharField(_('description'), max_length=255)

    content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
    object_id = models.PositiveIntegerField(_('object id'), db_index=True)
    object = generic.GenericForeignKey('content_type', 'object_id')

class ItemAccountAmountRef(Item):
    """ Items of which a Quote or an Invoice exists. """
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    reference = models.CharField(max_length=200)
    debit_account = models.ForeignKey(Account, related_name='receivables_receipt_debit_account')
    credit_account = models.ForeignKey(Account, related_name='receivables_receipt_credit_account')

class PaymentItem(ItemAccountAmountRef):
    pass

class Payment(models.Model):
    invoice = models.ManyToManyField(Invoice, null=True, blank=True)
    date = models.DateField('date')
    attachments = generic.GenericRelation(Attachment)
    site = models.ForeignKey(Site, related_name='payment_site', null=True, blank=True
    items = generic.GenericRelation(PaymentItem)

in the admin.py:

class PaymentItemInline(generic.GenericTabularInline):
    model = PaymentItem
    form = PaymentItemForm

class PaymentAdmin(admin.ModelAdmin):
    inlines = [PaymentItemInline]

in forms.py:

class PaymentItemForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(PaymentItemForm, self).__init__(*args, **kwargs)
        self.fields['credit_account'].label = "Bank Account"

In the PaymentItemInline the label is not changing. I have tried changing other attributes e.g. class which work. If I run through the init in debug mode I can see that the label variable is changing however when the form is rendered the field is still labelled credit account. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

心不设防 2024-12-10 10:27:57

你已经成功了 98%。不要尝试在 __init__ 中修改表单字段,只需在 ModelForm 中重新定义它即可。如果你将它命名为相同的东西,django 将能够弄清楚它应该验证 &保存到 ForeignKey 字段。您可以使用相同的公式来完全更改 ModelForm 中给定字段的字段或小部件。

您可以在此处找到每种模型字段类型的默认表单字段类型: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#field-types

class PaymentItemForm(forms.ModelForm):
    credit_account = forms.ModelChoiceField(label="Bank Account", queryset=Account.objects.all())

就是这样。根本不需要重写任何函数:)

顺便说一句,这个字段的文档在这里:https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield

You're 98% of the way there. Instead of trying to futz with the form field in __init__, just redefine it in your ModelForm. If you name it the same thing, django will be able to figure out that it is supposed to validate & save to the ForeignKey field. You can use the same formula to change a Field or Widget completely for a given field in a ModelForm.

You can find the default form field types for each model field type here: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#field-types

class PaymentItemForm(forms.ModelForm):
    credit_account = forms.ModelChoiceField(label="Bank Account", queryset=Account.objects.all())

That's it. No need to override any functions at all : )

Incidentally, the docs for this field are here: https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield

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