I want to know if this behavior has sense and which is the better way
to implement it, also if django developers would take care about it.

The problem is related to modelformsets, by default initial forms are
shown first in the formset, but what to do if I would place it in the
middle or at the lasts?

My case is that I have a model with a field DateField, I want to
display a modelformset that display one form for each month in a year,
including instanced form of my saved models in that year, but maybe
there are months without a model saved, so need use extra forms, for
example in my database I have 1 model:
{
    "pk": 1,
    "model": "myapp.month",
    "fields": {
        "date": "2013-02-01"
        "other_field": "some value"
    }
}

I created the formset:
YearMonthsFormSet = modelformset_factory(
    Month,
    extra=12,
    max_num=12,
)

And I want the formset display defaults values in each form, so I
create the initial list:
Initial = [
    {
        ‘date’: datetime.date(year=2013, month=1, day=1),
        ‘other_field’: ‘bla bla’
    },
    {}, # Since saved model month is February, left empty to display
it
    {
        ‘date’: datetime.date(year=2013, month=3, day=1),
        ‘other_field’: ‘more bla bla’
    },
    …
]

So in the view I create the formset:
formset = YearMonthsFormSet(queryset=
Month.objects.filter(date__year=2013).order_by('date'),
initial=initial)

When display the formset in the page the fields that should display my
saved model are empties, I notice that BaseModelFormSet construct
forms in a way that the first form is the form model in the queryset,
and then this form is overwritten by the specified initial list.

I think the behavior must be find the holes in initial and put the
modelforms in that holes instead in the first places, in this way I
can place instanced forms models in the position that I chose. It make
sense or there are a way to accomplish it that I missed?

I start to subclass BaseModelFormSet overwritten the _construct_form
method, but I notice that it wasn’t enough since other methods in both
BaseModelFormSet  and BaseFormSet use the same conception, and I
haven’t time to rethink, reprogram, test, etc. all those methods.

Finally I left the default behavior and reorder the formset in the
template, but this is not an good solution.

Any comments?
Thanks
Julio Cesar

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to