Как я могу добавить атрибут класса в поле formset, где мы используем функцию inlineformset_factory - PullRequest
0 голосов
/ 28 марта 2019

Если я добавлю поле unit_price в SupplierForm, оно отразится на моем шаблоне и добавит атрибут класса, но добавит обе формы. Я хочу переопределить форму записей только для unit_price. Как я могу это сделать.

class SupplierForm(forms.ModelForm):
    # unit_price = forms.FloatField(widget=forms.TextInput(
    #         attrs={
    #         'class':'product_price',
    #         }
    #     ))

    # VAT = forms.FloatField(widget=forms.TextInput(
    #         attrs={
    #         'class':'product_vat',
    #         }
    #     ))

    class Meta:
        model = Supplier
        exclude = ['uploaded_by', 'approved_by','unit_price']
        labels = {
        "payment_due_date": "Payment Due Date / Paid Date"
         }
        help_texts = {
            'invoice_date': '<b>Click on arrow for calendar</b>',
            'payment_due_date': '<b>Click on arrow for calendar</b>',
        }
        widgets = {
            'invoice_date': DateInput(format="%d %b %Y"),
            'payment_due_date':DateInput(),

        }

# I have added here unit_price field for add class attribute in this field but there is no reflect on template
class EnteriesForm(ModelForm):
    unit_price = forms.FloatField(widget=forms.TextInput(
            attrs={
            'class':'product_price',
            }
        ))
    class Meta:
        model = Enteries
        exclude = ()
        help_texts = {
            'unit_price': '<b>Click on arrow for calendar</b>',

        }

EnteriesFormSet = inlineformset_factory(Supplier, Enteries,
                                            form=SupplierForm,exclude=['uploaded_by'],extra=1)

1 Ответ

0 голосов
/ 29 марта 2019

Вы можете найти его здесь , но вы просто измените свой form. И как только вы объявляете form, вам больше не нужно использовать fields или exclude при объявлении formset, потому что все это должно быть установлено в вашем form

class EnteriesForm(ModelForm):
    unit_price = forms.FloatField(widget=forms.TextInput(
            attrs={
            'class':'product_price',
            }
        ))
    class Meta:
        model = Enteries
        exclude = ()
        help_texts = {
            'unit_price': '<b>Click on arrow for calendar</b>',
        }


EnteriesFormSet = inlineformset_factory(
    Supplier,
    Enteries,
    # this is where you select what form you want to use:
    form=EntriesForm,
    # 'uploaded_by' is not even apart of this form.
    # You should remove this.
    # exclude=['uploaded_by'],
    # 'extra': default is '1', so you don't really need this.
    # extra=1
)

Вы действительно должны вернуться и прочитать всю информацию по formsets. Наследование: formset -> modelformset -> inlineformset, поэтому все, что относится к formset, относится к inlineformset.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...