Snippet List
A formset class where you can add forms as you discover the need within your code. There is also the ability to add ManagmentForm fields.
If you ever found yourself in a situation where 1) you have repeated forms that need to be displayed in different locations, or 2) if you find the application logic works better if you add forms as you discover you need them, this code will help you out.
Below is pseudo code based on a real implementation I used. Each form had a save button and the SELECTED_PAYMENT field was set through JavaScript. It is very difficult to use JavaScript with repeated forms, without using a formset.
from myProject.myApp import myFormsUtils
from myProject.myApp.forms import PaymentForm
SELECTED_PAYMENT = 'SELECTED_PAYMENT'
# extra_fields format: {Field name: (Field type, Initial value)}
l_extra_fields = {SELECTED_PAYMENT: (forms.IntegerField, -1)}
PaymentFormSetType = myFormsUtils.formset_factory(PaymentForm, extra=0, extra_fields=l_extra_fields)
if request.method == 'POST':
paymentFormSet = PaymentFormSetType(data=request.POST)
if paymentFormSet.is_valid():
li_curFormIdx = pagaFormSet.management_form.cleaned_data[SELECTED_PAYMENT]
paymntForm = paymentFormSet.forms[li_curFormIdx]
... do stuff ...
# To generate the formset
paymentFormSet = PagamentoFormSetType()
# You can re-add a form retrieved (as in the one above)
l_form = paymentFormSet.add_form(paymntForm)
# Or use the add function just like creating a new form
l_form = paymentFormSet.add_form(personID=argPersonID, propID=argPropID, year=argYr, amt=lc_Amt)
I then stored the `l_form` variables above directly into a unique Context structure and displayed them each individually in my template. Of course this also meant that I also had to output the `paymentFormSet.management_form` explicitly within my template.
EDIT 09-11-2009: Modified the initial_form_count() method to properly handle initial form values in conjunction with dynamically added forms.
- form
- formset
- factory
- arbitrary-length
This is based on the Admin app functionality for dispatching calls.
Once you put these two files in place then add the following to urls.py:
from myProject import ajax
urlpatterns = patterns('',
...
# Add this to the urlpatterns list
(r'^ajax/(.*)', ajax.dispatcher.urls),
...)
you register a function or method with a name like so:
from django.contrib import ajax
def myAutoCompleteCall(request):
...
ajax.dispatcher.register('myAutoComplete', myAutoCompleteCall)
Then you can use the url: `http://www.mysite.com/ajax/myAutoComplete`
Previously I had placed this app in the django\\contrib directory because I wanted to use it in an Admin app mod. Since the release of 1.1 I was able to move it out into a standard app because of the new `formfield_overrides` property of the `ModelAdmin` class.
The BooleanField and DecimalField `elif` blocks are only included in this snippet to give context in the admin_list.py. Insert the CurrencyField block into the file and the Currency fields will display properly in record lists. If you have all of the objects ( [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Widget](http://www.djangosnippets.org/snippets/1526/), [Currency Form Field](http://www.djangosnippets.org/snippets/1527/), and the [Currency DB Field](http://www.djangosnippets.org/snippets/1528/) ) then all you have to do is use the DB Field object and the Admin app will (should) work properly.
Please let me know if you have any problems.
- internationalization
- admin
- i18n
- currency
- field
- babel
- decimal
This is an extension of the DecimalField database field that uses my [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Widget](http://www.djangosnippets.org/snippets/1526/), and [Currency Form Field](http://www.djangosnippets.org/snippets/1527/).
I placed my Currency object in the Django\\utils directory, the widget in Django\\froms\\widgets_special.py, and the form field in Django\\forms\\fields_special.py because I integrated this set of currency objects into the Admin app ( [here](http://www.djangosnippets.org/snippets/1529/) ) and it was just easier to have everything within Django.
UPDATE 08-18-2009: Added 'import decimal' and modified to_python slightly.
The rest of the series: [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Widget](http://www.djangosnippets.org/snippets/1526/), [Currency Form Field](http://www.djangosnippets.org/snippets/1527/), [Admin Integration](http://www.djangosnippets.org/snippets/1529/)
- internationalization
- i18n
- database
- currency
- field
- babel
- decimal
This is an extension of the DecimalField form field that uses my [Currency Object](http://www.djangosnippets.org/snippets/1525/) and [Currency Widget](http://www.djangosnippets.org/snippets/1526/).
I placed my Currency object in the Django\\utils directory and the widget in Django\\froms\\widgets_special.py because I integrated a set of currency objects into the Admin app ( [here](http://www.djangosnippets.org/snippets/1529/) ) and it was just easier to have everything within Django.
UPDATE 07-30-2009: Add the parse_string argument to properly test the string format as per the update to the [Currency Object](http://www.djangosnippets.org/snippets/1525/)
UPDATE 09-15-2009: Properly handle None's in the clean method
The rest of the series: [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Widget](http://www.djangosnippets.org/snippets/1526/), [Currency DB Field](http://www.djangosnippets.org/snippets/1528/), [Admin Integration](http://www.djangosnippets.org/snippets/1529/)
- internationalization
- i18n
- currency
- form
- field
- babel
- decimal
This is a simple TextInput widget that uses my [Currency object](http://www.djangosnippets.org/snippets/1525/).
I placed my Currency object in the Django utils directory because I integrated a set of currency objects into the Admin app ( [here](http://www.djangosnippets.org/snippets/1529/) ) and it was just easier to have everything within Django.
The rest of the series: [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Form Field](http://www.djangosnippets.org/snippets/1527/), [Currency DB Field](http://www.djangosnippets.org/snippets/1528/), [Admin Integration](http://www.djangosnippets.org/snippets/1529/)
- internationalization
- i18n
- currency
- babel
- decimal
This object stitches together the [Babel](http://babel.edgewall.org/) number formating and the Decimal object, with a little of my own hand rolled validation for parsing.
Note the comment at the end of the code. It contains two lines to add to your settings.py.
CURRENCY_LANGUAGE_CODE = 'pt_BR'
CURRENCY_CODE = '' # If one exists like 'USD', 'EUR'
UPDATE 06-03-2009: Now with rounding
UPDATE 07-14-2009: Now with - More graceful handling of missing settings variables - Support for negatives (small oversight) - More flexible format strings - Thorough doctest tests
UPDATE 07-30-2009: Added the parse_string argument to the `__new__()` method. This fixes a bug when importing data using `manage.py loaddata`. I have not yet updated the tests to reflect the change.
The rest of the series: [Currency Widget](http://www.djangosnippets.org/snippets/1526/), [Currency Form Field](http://www.djangosnippets.org/snippets/1527/), [Currency DB Field](http://www.djangosnippets.org/snippets/1528/), [Admin Integration](http://www.djangosnippets.org/snippets/1529/)
- internationalization
- i18n
- currency
- babel
- decimal
Rupe has posted 7 snippets.