2011-09-07 7 views
3

I models.pyに次のadmin.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) 

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

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

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

でPaymentItemInlineはラベルが変更されていないことを示します。私は他の属性を変更しようとしました。仕事をするクラス。デバッグモードでinitを実行すると、ラベル変数が変更されていることがわかりますが、フォームがレンダリングされてもフィールドはまだクレジットアカウントと表示されています。助言がありますか?

+0

は、ブラウザのキャッシュをクリアしましたか? – Pannu

+0

はい、私は持っていますが、それはまだ悲しいことに同じ結果を与えます。別のモデルでは、私は今、外出先のドロップダウンのonclickを変更しようとしましたが、インラインで2番目のインラインでのみ動作するようです。私はこれがジャンゴの問題かもしれないと感じていますが、私は間違っている可能性があります。今のところ、私はそうするために苦労するほどjavascriptを使ってこれらの変更を行う必要があるように見えます。 – Crazyconoli

+0

おそらくそれは* Djangoの問題ではないでしょう。ここに投稿したコードからは間違いはありませんが、オープンソース、コミュニティレビュー、徹底的にテストされた、大量展開のDjangoソースの前に自分のコードであると確信しています。ただ言って。 –

答えて

2

あなたはその道の98%です。 __init__のフォームフィールドでfutzを試す代わりに、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

関連する問題