A generic base class for extending ModelAdmin views. This can be used likewise:
def myview(self, request, object_id):
obj = self._getobj(request, object_id)
< do something >
def get_urls(self):
urls = super(MyAdmin, self).get_urls()
my_urls = patterns('',
url(r'^(.+)/myview/$',
self._wrap(self.myview),
name=self._view_name('myview')),
)
return my_urls + urls
- admin
- extending
- extendible
- custum-views
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