Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

getting things running

  • Loading branch information...
commit daf2a280913bf06799ca190d661b97dcbe95344c 1 parent c471622
Charles Leifer authored
1  example/admin.py
View
@@ -50,7 +50,6 @@ def get_context(self):
class MessageAdmin(ModelAdmin):
columns = ('user', 'content', 'pub_date',)
- exclude_filter_fields = ('user',)
related_filters = [QueryFilter(User.select(), exclude_fields=('password',))]
class NoteAdmin(ModelAdmin):
23 flask_peewee/filters.py
View
@@ -30,6 +30,15 @@ def process_formdata(self, valuelist):
raise ValueError(self.gettext(u'Invalid Choice: could not coerce'))
+class AjaxModelSelectField(fields.HiddenField):
+ def __init__(self, model, search, *args, **kwargs):
+ super(AjaxModelSelectField, self).__init__(*args, **kwargs)
+
+class AjaxModelSelectMultipleField(fields.HiddenField):
+ def __init__(self, model, search, *args, **kwargs):
+ super(AjaxModelSelectMultipleField, self).__init__(*args, **kwargs)
+
+
LOOKUP_TYPES = {
'eq': 'equal to',
'lt': 'less than',
@@ -70,9 +79,19 @@ def process_formdata(self, valuelist):
'datetime': ['today', 'yesterday', 'this_week', 'lte_days_ago', 'gte_days_ago', 'year_eq', 'year_lt', 'year_gt'],
}
+def handle_fk_single(lookup):
+ if lookup.related_lookup:
+ return AjaxModelSelectField(model=lookup.field.to, search=lookup.related_lookup)
+ return ModelSelectField(model=lookup.field.to)
+
+def handle_fk_multiple(lookup):
+ if lookup.related_lookup:
+ return AjaxModelSelectMultipleField(model=lookup.field.to, search=lookup.related_lookup)
+ return ModelSelectMultipleField(model=lookup.field.to)
+
CONVERTERS = {
- (ForeignKeyField, 'eq'): lambda l: AjaxModelSelectField(model=l.field.to, search=l.related_lookup),
- (ForeignKeyField, 'in'): lambda l: AjaxModelSelectMultipleField(model=l.field.to, search=l.related_lookup),
+ (ForeignKeyField, 'eq'): handle_fk_single,
+ (ForeignKeyField, 'in'): handle_fk_multiple,
(DateTimeField, 'today'): lambda l: fields.HiddenField(),
(DateTimeField, 'yesterday'): lambda l: fields.HiddenField(),
(DateTimeField, 'this_week'): lambda l: fields.HiddenField(),
5 flask_peewee/static/js/admin.js
View
@@ -47,7 +47,7 @@ var Admin = window.Admin || {};
ModelAdminFilter.prototype.display_lookup = function(row, field_name, lookup) {
var desired_elem = this.lookups_elem.find('#' + field_name + '__' + lookup);
if (desired_elem) {
- var clone = desired_elem.clone();
+ var clone = desired_elem.parents('span.wrapper').clone();
row.find('.lookup-input').remove();
row.append(clone);
return clone;
@@ -72,7 +72,8 @@ var Admin = window.Admin || {};
var row = this.add_filter(elem);
row.find('select').val(lookup);
- var input_elem = this.display_lookup(row, field, lookup);
+ var input_row = this.display_lookup(row, field, lookup)
+ , input_elem = input_row.find('.lookup-input');
input_elem.val(value);
}
}
2  flask_peewee/templates/admin/includes/filter_widgets.html
View
@@ -1,7 +1,7 @@
<div class="well" id="filter-wrapper" style="display: none;">
<div class="hidden" id="lookup-fields">
{% for model_lookup in query_filter.get_model_lookups() %}
- {% for field in model_lookup.html_form() %}{{ field(class="span5 input-small lookup-input") }}{% endfor %}
+ {% for field in model_lookup.html_form() %}<span class="wrapper">{{ field(class="span5 input-small lookup-input") }}</span>{% endfor %}
{% endfor %}
</div>
<form action="." class="form-inline modeladmin-filters" method="get">
Please sign in to comment.
Something went wrong with that request. Please try again.