Custom model manager chaining (Python 3 re-write)
This is a Python 3 re-write for https://djangosnippets.org/snippets/2117/
- manager
- queryset
- factory
This is a Python 3 re-write for https://djangosnippets.org/snippets/2117/
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.
This snippet is used to create a script for monitoring sphinx status with Nagios via [django-sphinx](http://code.google.com/p/django-sphinx/). It returns 0 (OK) or 2 (CRITICAL). Remember to change this strings `ModelToMonitor` and `app_name`. Usage : `./manage your-controls-command --log` > /your/script/name.py
3 snippets posted so far.