All - I've been struggling for a while trying to determine the Right
Way to present a form with a tabular inline subform that's grouped
using {% regroup %}.  I've tried the following:

1) Write a custom view - this failed due to general lack of skill with
Django.  Because I wanted the admin interface look, I extended the
template, but that required me to duplicate much of the context.
Since I end up duplicating most of the code in ModelAdmin anyway, I
tried

2) Subclassing the ModelAdmin - here the challenge was in rendering
the template.  I could pass the vanilla inline and of course Django
would render with the default tabular look, but I need to group the
inlines together.  The grouping field is actually two foreign keys
away - I want to group the "Score" inline rows by
"Score.Crit.CritArea.name" - and here I failed because I couldn't pass
the "Crit" or "CritArea" objects to the context.  Then several days
doing

3) A hacktastick amalgam of 1) and 2).

Needless to say, nothing worked.  I'll post the code for 2) below
because it felt like the most promising avenue.  Has anyone been able
to write grouped inlines?

---
#admin.py

class ScoresInline(admin.TabularInline):
        model = Score

class SheetAdmin(admin.ModelAdmin):
        date_hierarchy = 'dispatch_date_time'
        change_form_template = 'dcm/admin/sheet/change_form.html'

        def get_score_list(self):
                sl = []
                for s in self.scores:
                        sl.append( {
                                'id': s.id,
                                'crit_area_weight': s.crit.crit_area.weight,
                                'crit_area_name': s.crit.crit_area.name,
                                'crit_text': s.crit.text,
                                'weight': s.weight
                        })
                return sl

        def get_scores(self):
                return self.scores.values()

        def get_sheet_list(self):
                return Sheet.objects.filter(pk=self.sheet.id).values()

        def change_view(self, request, object_id, extra_content=None):
                self.sheet = get_object_or_404(Sheet, pk=object_id)
                self.scores = self.sheet.score_set.order_by('weight')
                sheet_context = {
                        'score_list': self.get_score_list(),
                        'scores': self.get_scores(),
                        'sheet_list': self.get_sheet_list()
                }

                return super(SheetAdmin, self).change_view(request, object_id,
extra_context = sheet_context)

        fieldsets = (
                ('Dispatch Call', {
                        'fields':
('dispatch_user','dispatch_date_time','location','call_type')
                }),
                ('Monitoring Session', {
                        'fields':
('monitor_user','monitor_date_time','recorded','monitor_comments')
                }),
                ('Coaching Session', {
                        'fields':
('coach_user','coached','coach_date_time','coach_comments'),
                        'classes': ('collapse',)
                }),
                ('Custom Sheet Template', {
                        'fields': ('custom_sheet',),
                        'classes':('collapse',)
                })
        )
        inlines = [ScoresInline,]
---
#change_form.html

{% extends 'admin/change_form.html' %}
{% load i18n admin_modify adminmedia %}

{% block title %}Dispatch Call Monitoring Scoresheet{% endblock %}


{% block content %}
<div id="content-main">
{% block form_top %}<h1>{{sheet}}</h1>{% endblock %}
        <form enctype="multipart/form-data" action="" method="post"
id="scoresheet_form">
                <div>
                {% if is_popup %}<input type="hidden" name="_popup" value="1" 
/>{%
endif %}
                {% if save_on_top %}{% submit_row %}{% endif %}
                {% if errors %}
                        <p class="errornote">
                        {% blocktrans count errors|length as counter %}Please 
correct the
error below.{% plural %}Please correct the errors below.{%
endblocktrans %}
                        </p>
                        <ul class="errorlist">{% for error in
adminform.form.non_field_errors %}<li>{{ error }}</li>{% endfor %}</
ul>
                {% endif %}

                {% for fieldset in adminform %}
                  {% include "admin/includes/fieldset.html" %}
                {% endfor %}

                {% block after_field_sets %}{% endblock %}

                {% for inline_admin_formset in inline_admin_formsets %}
                        {% include inline_admin_formset.opts.template %}
                {% endfor %}

                        {% block after_related_objects %}{% endblock %}

                        {% submit_row %}

                        {% if add %}
                        <script type="text/javascript">document.getElementById
("{{ adminform.first_field.auto_id }}").focus();</script>
                        {% endif %}

                        {# JavaScript for prepopulated fields #}
                        {% prepopulated_fields_js %}
                </div>
        </form>
</div>

</div>
</form></div>
{% endblock %}

#TemplateSyntaxError is "'int' object not iterable" at the
"inline_admin_form" line, and my best guess is that I haven't been
able to declare the formset and hook it to the model correctly


Thanks very much in advance for any ideas or suggestions -
Ben

--~--~---------~--~----~------------~-------~--~----~
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