django modelform property hidden field

mastachimp

I'm manually displaying my formset as a table, with each form being looped over. At the bottom of each form I include the hidden fields like:

{% for hidden in form.hidden_fields %}
    {{ hidden }}
{% endfor %}

But the problem is that I am also including properties in my form like:

class AllocationForm(forms.ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={'size': '15'}))

    def __init__(self, *args, **kwargs):
        super(AllocationForm, self).__init__(*args, **kwargs)
        if self.instance:
            self.fields['total_budgeted'] = self.instance.total_budgeted()
            self.fields['total_budgeted_account_percent'] = self.instance.total_budgeted_account_percent()
            self.fields['actual_spent'] = self.instance.actual_spent()
            self.fields['actual_spent_account_percent'] = self.instance.actual_spent_account_percent()
            self.fields['total_budgeted_category_percent'] = self.instance.total_budgeted_category_percent()
            self.fields['actual_spent_category_percent'] = self.instance.actual_spent_category_percent()

    class Meta:
        model = Allocation
        exclude = {'created', 'modified', 'source_account'}

And this works in the sense that I definitely see the properties being called, however they display as nothing so that's another issue.

The problem is when I keep the hidden fields in the template I will get errors such as 'int' object has no attribute 'get_bound_field' and so on depending on the return type of the property/method call.

My question is first: is there a check I can do to see if the field is a property in the template and therefore skip over it? It may have something to do with how I'm using the property since in fact every property is displaying nothing (but I see it callback), so second would be about how to display the properties.

hisie

Well I am in the next step of the problem, but i have success in generating true form fields. In place of:

if self.instance:
    self.fields['total_budgeted'] = self.instance.total_budgeted()

You can write:

if self.instance:
    self.fields['total_budgeted'] = form.CharField(
        initial=self.instance.total_budgeted(),
        widget=HiddenInput()
    )

In this code you I instantiate the form Field as CharField, you can use the FormField you want, and I hide it by choosing the Hidden input widget.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to hide a field in django modelform?

From Dev

Django ModelForm not showing field errors

From Dev

Save custom field in Django ModelForm

From Dev

Django creation ModelForm with OneToOneField hidden but required

From Dev

Django creation ModelForm with OneToOneField hidden but required

From Dev

"This field cannot be null" error in a django 1.5 ModelForm

From Dev

Dynamically alter Field choices in Django ModelForm

From Dev

Add class to form field Django ModelForm

From Dev

Django - ModelForm: Add a field not belonging to the model

From Dev

filter choices for many to many field in modelform django

From Dev

Django ModelForm make field from other fields

From Dev

Django - accessing modelform field value in template

From Dev

Set value of field for Django ModelForm in CreateView

From Dev

Overriding ModelForm field error messages in Django 1.6

From Dev

Django CreateView ModelForm dropdown field queryset filter

From Dev

Django - Assign default value to field in ModelForm

From Dev

Add field to django ModelForm that are in the model but not in the form

From Dev

Dynamically excluding field from Django ModelForm

From Dev

Django - Validate a disabled field in modelform_factory

From Dev

Cannot set excluded field in ModelForm in Django 1.4

From Dev

Overriding ModelForm field error messages in Django 1.6

From Dev

Set value of field for Django ModelForm in CreateView

From Dev

How to add a form as field attribute in a django ModelForm

From Dev

Add and initialize custom field in Django ModelForm

From Dev

Django 1.11 pass kwarg to modelform field

From Dev

Validating dynamically created ModelForm field in Django 2

From Dev

Django ModelForm float field not calling Clean_Field method

From Dev

Django ModelForm won't allow Null value for required field?

From Dev

How to make a modelform editable foreign key field in a django template?

Related Related

  1. 1

    How to hide a field in django modelform?

  2. 2

    Django ModelForm not showing field errors

  3. 3

    Save custom field in Django ModelForm

  4. 4

    Django creation ModelForm with OneToOneField hidden but required

  5. 5

    Django creation ModelForm with OneToOneField hidden but required

  6. 6

    "This field cannot be null" error in a django 1.5 ModelForm

  7. 7

    Dynamically alter Field choices in Django ModelForm

  8. 8

    Add class to form field Django ModelForm

  9. 9

    Django - ModelForm: Add a field not belonging to the model

  10. 10

    filter choices for many to many field in modelform django

  11. 11

    Django ModelForm make field from other fields

  12. 12

    Django - accessing modelform field value in template

  13. 13

    Set value of field for Django ModelForm in CreateView

  14. 14

    Overriding ModelForm field error messages in Django 1.6

  15. 15

    Django CreateView ModelForm dropdown field queryset filter

  16. 16

    Django - Assign default value to field in ModelForm

  17. 17

    Add field to django ModelForm that are in the model but not in the form

  18. 18

    Dynamically excluding field from Django ModelForm

  19. 19

    Django - Validate a disabled field in modelform_factory

  20. 20

    Cannot set excluded field in ModelForm in Django 1.4

  21. 21

    Overriding ModelForm field error messages in Django 1.6

  22. 22

    Set value of field for Django ModelForm in CreateView

  23. 23

    How to add a form as field attribute in a django ModelForm

  24. 24

    Add and initialize custom field in Django ModelForm

  25. 25

    Django 1.11 pass kwarg to modelform field

  26. 26

    Validating dynamically created ModelForm field in Django 2

  27. 27

    Django ModelForm float field not calling Clean_Field method

  28. 28

    Django ModelForm won't allow Null value for required field?

  29. 29

    How to make a modelform editable foreign key field in a django template?

HotTag

Archive