Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Label Generic Inline Admin

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?

like image 444
Crazyconoli Avatar asked Jun 10 '26 13:06

Crazyconoli


1 Answers

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

like image 101
Dave Avatar answered Jun 13 '26 05:06

Dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!