Closed marcinowski closed 6 years ago
Merging #11 into master will increase coverage by
0.1%
. The diff coverage is100%
.
@@ Coverage Diff @@
## master #11 +/- ##
=========================================
+ Coverage 96.64% 96.75% +0.1%
=========================================
Files 6 6
Lines 268 277 +9
=========================================
+ Hits 259 268 +9
Misses 9 9
Impacted Files | Coverage Δ | |
---|---|---|
rangefilter/filter.py | 93.06% <100%> (+0.67%) |
:arrow_up: |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact)
,ø = not affected
,? = missing data
Powered by Codecov. Last update e45ec65...54eaf60. Read the comment docs.
Good solution, but not works with two filtres.
Test on:
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('name', 'created_at')
list_editable = ('created_at',)
list_filter = (
'name', ('created_at', DateRangeFilter), ('updated_at', DateRangeFilter),
)
Works for me just fine, I'm on Django 1.9.6.
Edit: Although I've modified the template a little, so that may have some impact. I can try checking it more over the weekend.
Problem:
If there are no editable fields, the number of load scripts is not an problem.
I'm back! :D I see that this issue is still not resolved, so I thought I'd get back to it. This time I've tested the solution on Django 2.0.5, the solution also seems a bit better, although I'm not 100% why this works. I've tested this on the following
from django.db import models
from django.contrib import admin
from rangefilter.filter import DateRangeFilter
class Post(models.Model):
from_time = models.DateTimeField()
to_time = models.DateTimeField()
class Comment(models.Model):
from_time = models.DateField()
to_time = models.DateField()
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('from_time', 'to_time')
list_editable = ('from_time', 'to_time')
list_display_links = None
list_filter = (
('from_time', DateRangeFilter), ('to_time', DateRangeFilter)
)
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ('from_time', 'to_time')
list_editable = ('from_time', 'to_time')
list_display_links = None
list_filter = (
('from_time', DateRangeFilter), ('to_time', DateRangeFilter)
)
Edit: I see that the coverage failed, if someone else can confirm manually this works, then I'll add some units
Edit2: of course it fails, it was too easy
Ok, I've found the issue myself, commenting out the list_editable
removes the widget from filters
, damn it
Ok, I think I've finally figured it out. Here's how it worked:
First Django loads the model formset with predefined widgets for different field types. If there's a date based field, then it loads the AdminDateWidget and it's required media to context under {{media.js}}
in _templates/admin/changelist.html. (Note: it accumulates media in django.forms.widgets.Media object, which prevents duplicates, but the DateRangeFilter is not included yet since it's not model field related. List of predefined widgets is in _django.contrib.admin.options.FORMFIELD_FOR_DBFIELDDEFAULTS)
After that Django starts rendering forms, which have the {{form.media}}
tag. Only then the DjangoRangeFilter.get_media()
is called and rendered, which creates the duplicates.
How it works now:
window.DateTimeShortcuts
. Otherwise, the window.DateTimeShortcuts
is undefined.DateTimeShortcuts
has been set and if not then the DateTimeShortcuts.js and calendar.js is renderedDJANGO_RANGEFILTER_ADMIN_JS_SET
on request.How it could work:
DateTimeShortcuts.js
the init
method is attached to the eventListener on loads
. It doesn't check if it has been done previously, that's why we get multiple identical objects listening on loads
. So in admin/js/admin/DateTimeShortcuts.js::423
instead of
window.addEventListener('load', DateTimeShortcuts.init);
window.DateTimeShortcuts = DateTimeShortcuts;
it should be
if (!('DateTimeShortuts' in window)) {
window.addEventListener('load', DateTimeShortcuts.init);
window.DateTimeShortcuts = DateTimeShortcuts;
}
Tested on Chrome & Firefox on Django 2.0.5 (I can test on 1.8 tomorrow) with the following code:
from django.db import models
from django.contrib import admin
from rangefilter.filter import DateRangeFilter, DateTimeRangeFilter
class Post(models.Model):
from_time = models.DateTimeField()
to_time = models.DateTimeField()
class Comment(models.Model):
from_time = models.DateField()
to_time = models.DateField()
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('from_time', 'to_time')
list_display_links = None
list_filter = (
('from_time', DateTimeRangeFilter),
('to_time', DateTimeRangeFilter),
('to_time', DateTimeRangeFilter),
('to_time', DateTimeRangeFilter),
('to_time', DateTimeRangeFilter),
)
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ('from_time', 'to_time')
list_editable = ('from_time', 'to_time')
list_display_links = None
list_filter = (
('from_time', DateRangeFilter),
('to_time', DateRangeFilter),
('to_time', DateRangeFilter),
('to_time', DateRangeFilter),
('to_time', DateRangeFilter),
)
AdminDateFilter included in editable
Statics loaded:
AdminDateFilter not included in editable
Unbelievable! It's work! 🎉 (1.8 / 2.0) @marcinowski thank you very much for the work done!
Should fix https://github.com/silentsokolov/django-admin-rangefilter/issues/9 I've made this quick fix today, but can test it a bit more tomorrow though.