Improve the rendering of inline form sets

This commit is contained in:
Andrew Ingram 2013-05-22 01:06:25 +01:00
parent 05f69775b2
commit a1ce27d4c2
7 changed files with 76 additions and 51 deletions

View file

@ -11,6 +11,8 @@ from django.conf.urls import patterns, url
from django.contrib.auth import models as auth_app
from django.db.models import get_models, signals
import extra_views
from djadmin2 import apiviews
from djadmin2 import views
@ -248,6 +250,23 @@ class ModelAdmin2(BaseAdmin2):
return self.get_api_urls(), None, None
class Admin2Inline(extra_views.InlineFormSet):
"""
A simple extension of django-extra-view's InlineFormSet that
adds some useful functionality.
"""
def construct_formset(self):
"""
Overrides construct_formset to attach the model class as
an attribute of the returned formset instance.
"""
formset = super(Admin2Inline, self).construct_formset()
formset.model = self.inline_model
return formset
class ImmutableAdmin(object):
"""
Only __init__ allows setting of attributes

View file

@ -1,21 +0,0 @@
{% extends "admin2/bootstrap/base.html" %}
{% block title %}Add {{ model }}{% endblock %}
{% block page_title %}Add {{ model }}{% endblock %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
{% for formset in inlines %}
{{ formset }}
{% endfor %}
<input type="submit"/>
</form>
{% endblock content %}

View file

@ -1,23 +0,0 @@
{% extends "admin2/bootstrap/base.html" %}
{% block title %}Change {{ model }}{% endblock %}
{% block page_title %}Change {{ model }}{% endblock %}
{% block content %}
<div class="row">
<div class="span12">
<form method="post">
{% csrf_token %}
{{ form.as_p }}
{% for formset in inlines %}
{{ formset }}
{% endfor %}
<input type="submit"/>
</form>
</div>
</div>
{% endblock content %}

View file

@ -1,5 +1,7 @@
{% extends "admin2/bootstrap/base.html" %}
{% load admin2_tags %}
{% block title %}{{ action }} {{ model }}{% endblock %}
{% block page_title %}{{ action }} {{ model }}{% endblock %}
@ -13,8 +15,34 @@
{{ form.as_p }}
{% for formset in inlines %}
{{ formset }}
{% endfor %}
<h4>{{ formset.model|model_verbose_name_plural|capfirst }}</h4>
{{ formset.management_form }}
<table class="table table-striped table-bordered">
<thead>
<tr>
{% for field in formset|formset_visible_fieldlist %}
<th>{{ field }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for inline_form in formset %}
<tr>
{% for field in inline_form.visible_fields %}
<td>
{% if forloop.first %}
{% for hidden_field in inline_form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% endif %}
{{ field }}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endfor %}
<button class="btn btn-small" type="submit" name="_addanother">Save and add another</button>
<button class="btn btn-small" type="submit" name="_continue">Save and continue editing</button>

View file

@ -9,3 +9,27 @@ def admin2_urlname(view, action):
Converts the view and the specified action into a valid namespaced URLConf name.
"""
return 'admin2:%s_%s_%s' % (view.app_label, view.model_name, action)
@register.filter
def model_verbose_name(obj):
"""
Returns the verbose name of a model instance or class.
"""
return obj._meta.verbose_name
@register.filter
def model_verbose_name_plural(obj):
"""
Returns the pluralized verbose name of a model instance or class.
"""
return obj._meta.verbose_name_plural
@register.filter
def formset_visible_fieldlist(formset):
"""
Returns the labels of a formset's visible fields as an array.
"""
return [f.label for f in formset.forms[0].visible_fields()]

View file

@ -135,7 +135,7 @@ class ModelEditFormView(AdminModel2Mixin, Admin2ModelFormMixin, extra_views.Upda
return context
class ModelAddFormView(AdminModel2Mixin, Admin2ModelFormMixin, generic.extra_views.CreateWithInlinesView):
class ModelAddFormView(AdminModel2Mixin, Admin2ModelFormMixin, extra_views.CreateWithInlinesView):
form_class = None
default_template_name = "model_update_form.html"
permission_type = 'add'

View file

@ -5,10 +5,8 @@ from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import Group, User
from rest_framework.relations import PrimaryKeyRelatedField
import extra_views
import djadmin2
from djadmin2.models import ModelAdmin2
from djadmin2.models import ModelAdmin2, Admin2Inline
from djadmin2.apiviews import Admin2APISerializer
@ -31,7 +29,7 @@ class UserSerializer(Admin2APISerializer):
exclude = ('passwords',)
class CommentInline(extra_views.InlineFormSet):
class CommentInline(Admin2Inline):
model = Comment