1

I need an integer form field to have a 'default' value. But I need to define this value at runtime.

I do it like ths:

# at my form constructor:
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList,
             label_suffix=':', empty_permitted=False):
    super(FlatSearchForm, self).__init__(data, files, auto_id, prefix, initial, error_class, label_suffix,
        empty_permitted)

    min_price = ... # some code to find the minimum price
    self.price_min = min_price # will be used later

    max_price = ... # some code to find the maximum price
    self.price_max = max_price

    # here we go:
    self.fields['price_from'] = forms.IntegerField(min_value=min_price, max_value=max_price, initial=min_price)
    self.fields['price_to'] = forms.IntegerField(min_value=min_price, max_value=max_price, initial=max_price)

# ...
# how I use the form (in views.py):
class SearchFlatView(ListView):
    model = Flat
    context_object_name = 'flats'
    template_name = 'catalog/search.html'

    def get_context_data(self, **kwargs):
        context = super(SearchFlatView, self).get_context_data(**kwargs)
        context['form'] = FlatSearchForm(self.request.GET)
        return context

The problem is: values are not shown in the form when I render the field using {{form.price_from}} and {{form.price_to}} - these fields are just empty!

Can you please tell me what I am doing wrong?

2
  • How do you use the form? How do you instantiate it? Is it bound or unbound form? Commented Jul 13, 2012 at 8:34
  • updated the code. check it out. Commented Jul 13, 2012 at 8:40

1 Answer 1

1

You cannot set dynamically initial values for fields in a bound form. See the docs: https://docs.djangoproject.com/en/dev/ref/forms/fields/#initial

The initial argument lets you specify the initial value to use when rendering this Field in an unbound Form.

What you can do however is to create an unbound form, with still preserving the request.GET params:

initial = request.GET.copy()
initial['price_from'] = some_min_price
initial['price_to'] = some_max_price
context['form'] = FlatSearchForm(initial=initial)

While in the form __iniit__ you'll have:

def __init__(self, *args, **kwargs):
    super(Form, self).__init__(*args, **kwargs)
    initial = kwargs.get("initial", {})
    default_min_value = ... #  some default value in case there is no `initial` passed
    default_max_value = ...
    self.fields['price_from'] = forms.IntegerField(min_value=initial.get('min_value', default_min_value), max_value=initial.get('max_value', default_max_value))
    self.fields['price_to'] = forms.IntegerField(min_value=initial.get('min_value', default_min_value), max_value=initial.get('max_value', default_max_value))

Or you may want to change this logic, if needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.