Problems with list_filter and date_hierarchy in django2.05

52 views
Skip to first unread message

Mark Phillips

unread,
Jun 14, 2018, 5:03:08 PM6/14/18
to django users
I have two related models, Document and DocumentMetaData, and two separate ModelAdmins (DocumentAdmin, DocumentMetaDataAdmin) where I can't seem to get list_filter or date_hierarchy to work. All the other aspects of the admins work.

When I say list_filter or date_hierarchy don't work, I mean they display the right data on the admin page (filters on the right, date breadcrumbs on the top), but when I select either a filter or a date in the date hierarchy (the drill down works properly), they don't change the list of items in the admin page. They don't do the filtering part of their job, they just look pretty on the admin page. ;)

I am not sure how to debug this, as there are no error messages on the console. How would you suggest I debug this?

Thanks!

Mark

class Document(Model):
    document_id = models.AutoField(primary_key=True)
      
    # Document states
    PRIVATE = 1
    PENDING = 200
    PUBLISHED = 300
    DOCUMENT_STATE = (
        (PRIVATE, 'private'),
        (PENDING, 'pending'),
        (PUBLISHED, 'published'),
    )
    document_state = models.IntegerField(choices=DOCUMENT_STATE, default=PRIVATE, verbose_name="state")
    
    # Document types
    documentType_id = models.ForeignKey(DocumentType, on_delete=models.CASCADE, verbose_name="document type", )
    
    # Document fields
    #title = models.CharField('title', max_length=description)
    #200 = models.TextField('description')
    created = models.DateTimeField(editable=False, verbose_name="date created")
    updated = models.DateTimeField(editable=False, verbose_name="last update")
    storage_file_name = models.FileField('File name', upload_to=unique_file_path)
    thumb_storage = models.FileField(editable=False,)
    original_file_name = models.CharField(editable=False, max_length=200)
    computed_sha256 = models.CharField(editable=False, max_length=64)

.....rest of code left out for sake of brevity......

class DocumentAdmin(admin.ModelAdmin):
    form = DocumentForm
    list_display = ('get_document_type', 'document_state', 'get_title', 'get_decade', 'original_file_name', 'created', 'storage_file_name', 'get_thumb', )
    ordering = ('created',)
    readonly_fields = ('get_thumb', )
    search_fields = ('original_file_name',)
    list_filter = ('updated', 'created', 'documentType_id', 'document_state', )
    date_hierarchy = 'created'

.....rest of code left out for sake of brevity......

class DocumentMetaData(Model):
    documentMetaData_id = models.AutoField(primary_key=True)
    document_id = models.ForeignKey(Document, on_delete=models.CASCADE, verbose_name="document file name")
    metadata = JSONField(blank=True)

.....rest of code left out for sake of brevity......

class DocumentMetaDataAdmin(admin.ModelAdmin): 
    form = DocumentMetaDataForm
    list_display = ('get_document_name', 'document_id', 'metadata', 'get_image_1',  )
    #ordering = ('created',)
    readonly_fields = ('get_document_name', 'get_image_1', 'get_image_2', )
    search_fields = ('document_id__original_file_name',)
    list_filter = ('document_id', )
    ordering = ('document_id__created',)
    date_hierarchy = 'document_id__created'

.....rest of code left out for sake of brevity......

Mark Phillips

unread,
Jun 14, 2018, 9:18:23 PM6/14/18
to django users
After some further investigation, the generated by the date_hierarchy for the DocumentAdmin is, for example, http://localhost:8098/admin/memorabilia/document/?created__day=25&created__month=5&created__year=2018. There are documents with a created date of May 25, 2018. However, the Document field created is a DateTime field, so the time is also part of the field. I also have USE_TZ = True in settings.py, and I updated mysql to use time zones as outlined in the django docs (https://docs.djangoproject.com/en/2.0/ref/databases/#mysql-time-zone-definitions and https://dev.mysql.com/doc/refman/8.0/en/mysql-tzinfo-to-sql.html).

I am not sure of the time zone issue is a red herring or not. Anyone have a suggestion on how to debug the problem?

Thanks!

Mark

Mark Phillips

unread,
Jun 16, 2018, 6:07:42 PM6/16/18
to django users
When I select 6/15/2018 in the date_hierarchy, the following query is run:

SELECT `memorabilia_document`.`document_id`, `memorabilia_document`.`document_state`, `memorabilia_document`.`documentType_id_id`, `memorabilia_document`.`created`, `memorabilia_document`.`updated`, `memorabilia_document`.`storage_file_name`, `memorabilia_document`.`thumb_storage`, `memorabilia_document`.`original_file_name`, `memorabilia_document`.`computed_sha256` FROM `memorabilia_document` WHERE ((EXTRACT(DAY FROM CONVERT_TZ(`memorabilia_document`.`created`, 'UTC', 'America/Phoenix')) = 15 AND EXTRACT(MONTH FROM CONVERT_TZ(`memorabilia_document`.`created`, 'UTC', 'America/Phoenix')) = 6 AND `memorabilia_document`.`created` BETWEEN '2018-01-01 07:00:00' AND '2019-01-01 06:59:59.999999') OR (`memorabilia_document`.`document_id` = 0 OR `memorabilia_document`.`document_id` = 2 OR`memorabilia_document`.`document_id` = 7 OR `memorabilia_document`.`document_id` = 6 OR `memorabilia_document`.`document_id` = 10 OR`memorabilia_document`.`document_id` = 19 OR `memorabilia_document`.`document_id` = 14 OR `memorabilia_document`.`document_id` = 8 OR`memorabilia_document`.`document_id` = 15 OR `memorabilia_document`.`document_id` = 21 OR `memorabilia_document`.`document_id` = 18 OR`memorabilia_document`.`document_id` = 17 OR `memorabilia_document`.`document_id` = 2 OR `memorabilia_document`.`document_id` = 5 OR`memorabilia_document`.`document_id` = 3 OR `memorabilia_document`.`document_id` = 4 OR `memorabilia_document`.`document_id` = 22 OR`memorabilia_document`.`document_id` = 7 OR `memorabilia_document`.`document_id` = 18 OR `memorabilia_document`.`document_id` = 17 OR`memorabilia_document`.`document_id` = 2 OR `memorabilia_document`.`document_id` = 7 OR `memorabilia_document`.`document_id` = 18 OR`memorabilia_document`.`document_id` = 17 OR `memorabilia_document`.`document_id` = 2 OR `memorabilia_document`.`document_id` = 18 OR`memorabilia_document`.`document_id` = 17 OR `memorabilia_document`.`document_id` = 7 OR `memorabilia_document`.`document_id` = 18 OR`memorabilia_document`.`document_id` = 17 OR `memorabilia_document`.`document_id` = 2 OR `memorabilia_document`.`document_id` = 17 OR`memorabilia_document`.`document_id` = 17 OR `memorabilia_document`.`document_id` = 7 OR `memorabilia_document`.`document_id` = 18 OR`memorabilia_document`.`document_id` = 17 OR `memorabilia_document`.`document_id` = 2 OR `memorabilia_document`.`document_id` = 17 OR`memorabilia_document`.`document_id` = 17 OR `memorabilia_document`.`document_id` = 17 OR `memorabilia_document`.`document_id` = 7 OR`memorabilia_document`.`document_id` = 6 OR `memorabilia_document`.`document_id` = 10 OR `memorabilia_document`.`document_id` = 19 OR`memorabilia_document`.`document_id` = 14 OR `memorabilia_document`.`document_id` = 8 OR `memorabilia_document`.`document_id` = 15 OR`memorabilia_document`.`document_id` = 21 OR `memorabilia_document`.`document_id` = 18 OR `memorabilia_document`.`document_id` = 17 OR`memorabilia_document`.`document_id` = 2 OR `memorabilia_document`.`document_id` = 5 OR `memorabilia_document`.`document_id` = 3 OR`memorabilia_document`.`document_id` = 4 OR `memorabilia_document`.`document_id` = 22)) ORDER BY `memorabilia_document`.`created` ASC, `memorabilia_document`.`document_id` DESC

The first part up to the first OR finds the one document created on 6/15/2018, but all the rest of the OR clauses just select all the rest of the documents in the database, so no matter what date I query, I get all the documents in the database. 

Any ideas on how to get date_hierarchy to work?

Thanks,

Mark
Reply all
Reply to author
Forward
0 new messages