fabiocaccamo / django-treenode

:deciduous_tree: probably the best abstract model/admin for your tree based stuff.
MIT License
669 stars 32 forks source link

treenode's sorting is not OK #14

Closed nicolasVaye closed 5 years ago

nicolasVaye commented 5 years ago

Hi,

i reproduce a directories treenode with name's sorting and in a directory viewer the sorting seem to be OK but with treenode, it's not OK. It seem to me that there is a problem with letter and number.

see in attachments 2 screenshots, one to see the directory and the other to see in the treenode directory.

image

image

here is my pip list output : backports.csv (1.0.7) defusedxml (0.6.0) diff-match-patch (20181111) Django (2.1) django-debug-toolbar (1.11) django-extensions (2.1.7) django-import-export (1.2.0) django-js-asset (1.2.2) django-treenode (0.13.1) et-xmlfile (1.0.1) jdcal (1.4.1) odfpy (1.4.0) openpyxl (2.6.2) pip (9.0.1) pkg-resources (0.0.0) psycopg2-binary (2.8.2) pydotplus (2.0.2) pyparsing (2.4.0) pytz (2019.1) PyYAML (5.1) setuptools (32.3.1) six (1.12.0) sqlparse (0.3.0) tablib (0.13.0) wheel (0.33.4) xlrd (1.2.0) xlwt (1.3.0)

fabiocaccamo commented 5 years ago

@nicolasVaye thank you for reporting this. Could you share also your model and modeladmin implementation please?

nicolasVaye commented 5 years ago

here are the files.

models.py :

-- coding: utf-8 --

from django.db import models

from treenode.models import TreeNodeModel

class ItemType(): CLASS_IN_KEY_CHOICES = ( ('DATABASE', 'DATABASE'), ('ENV', 'ENV'), ('JARVIS', 'JARVIS'), ('REVERSEPROXY', 'REVERSEPROXY'), ('SERVER', 'SERVER'), ('SOFT', 'SOFT'), ('SOLR', 'SOLR'), ('TOMCAT', 'TOMCAT'), ) name = models.CharField(max_length=20,choices=CLASS_IN_KEY_CHOICES,unique=True,verbose_name="type d'item")

class Meta:
    verbose_name = "type d'item"
    verbose_name_plural = "types d'item"
    ordering = ['name',]

def __str__(self):
    return self.name

class ObjectType(): DESC_CLASS_IN_KEY_CHOICES = (
('DATABASE CORE SOLR', 'DATABASE CORE SOLR'), ('DATABASE ORACLE', 'DATABASE ORACLE'), ('DATABASE POSTGRES', 'DATABASE POSTGRES'), ('ENVIRONMENT', 'ENVIRONMENT'), ('JARVIS', 'JARVIS'), ('REVERSEPROXY', 'REVERSEPROXY'), ('SERVER', 'SERVER'), ('SOFT', 'SOFT'), ('SOLR (sgbd)', 'SOLR (sgbd)'), ('TOMCAT', 'TOMCAT'), )

item_type = models.ForeignKey('ItemType', on_delete=models.CASCADE, verbose_name="type", null=True, blank=True)
name = models.CharField(max_length=100,choices=DESC_CLASS_IN_KEY_CHOICES, default='description...',verbose_name="type d'objet")

class Meta:
    verbose_name = "type d'objet"
    verbose_name_plural = "types d'objet"
    ordering = ['name',]

def __str__(self):
    return self.name

class Object(TreeNodeModel):

name  = models.CharField(max_length=200, help_text='Le nom de l\'object',verbose_name="objet")
type  = models.ForeignKey('ObjectType', on_delete=models.CASCADE,verbose_name="type d'objet")
treenode_display_field = 'name'

class Meta(TreeNodeModel.Meta):
    verbose_name = 'Objet'
    verbose_name_plural = 'Objets'

def clean(self):
    if self.name == '### ejarvis ###':
        self.tn_parent = None
    elif self.tn_parent is None:
        raise ValidationError("Il faut rattacher l'objet à son parent !!")
    # elif self.type.item_type.name == self.tn_parent.type.item_type.name:
        # raise ValidationError("On ne peut pas attacher un objet avec un parent de même type !!")

and my admin.py :

-- coding: utf-8 --

from django.contrib import admin from import_export.admin import ImportExportModelAdmin

from .models import ObjectType, ItemType, Object

from treenode.admin import TreeNodeModelAdmin from treenode.forms import TreeNodeForm

@admin.register(ItemType) class ItemTypeAdmin(ImportExportModelAdmin): list_display = ('name',) ordering = ('name', ) search_fields = ('name',)

@admin.register(ObjectType) class ObjectTypeAdmin(ImportExportModelAdmin): list_display = ('name', 'item_type') list_filter = ('item_type__name',) ordering = ('item_type__name', ) search_fields = ('name',) list_select_related = ('item_type',)

class ObjectAdmin(TreeNodeModelAdmin):

# set the changelist display mode: 'accordion', 'breadcrumbs' or 'indentation' (default)
# when changelist results are filtered by a querystring,
# 'breadcrumbs' mode will be used (to preserve data display integrity)
treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_ACCORDION
#treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_BREADCRUMBS
#treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_INDENTATION

# use TreeNodeForm to automatically exclude invalid parent choices
form = TreeNodeForm
list_filter    = ('type__name',)
search_fields  = ('name',)

fieldsets = (
    # Fieldset 1 : meta-info (titre, auteur…)
    ('Général', {
        'classes': ['extrapretty', ],
        'fields': ('tn_parent', 'name', 'type',)
    }),
)

admin.site.register(Object, ObjectAdmin)

fabiocaccamo commented 5 years ago

Models are sorted alphabetically using __str__ value with respect to parent node.

If you want to manually customize ordering using the admin just add tn_order in your model admin list_display / list_editable.

Your problem has been fixed in 0.13.2, let me know if everything works correctly.

nicolasVaye commented 5 years ago

OK, thanks, it works fine with 0.13.2 and django 2.1.

It appears that with django 2.2.2, accordion display mode doesn't work but it's not this issue.

fabiocaccamo commented 5 years ago

Ok, I will debug it. Thanks for the report.

fabiocaccamo commented 5 years ago

@nicolasVaye fixed in 0.13.3 version.