Manually ordering items in relation to siblings

20 views
Skip to first unread message

Andre Terra

unread,
Jul 19, 2011, 1:58:57 PM7/19/11
to django...@googlegroups.com
Hello, everyone

Assume the following models (dpaste.com [1]):

class Account(models.Model):
    """
    Account model for categorizing entries from each SAP import,
    and eventually sorting, grouping, filtering data.
   
    """
    code = models.CharField(max_length=255, primary_key=True)
   
    group = models.ForeignKey('AccountGroup', blank=True, null=True,
                              related_name='groups')
   
    description = models.CharField(max_length=512, blank=True, null=True)
   
   
class AccountGroup(models.Model):
    name = models.CharField(max_length=512, blank=True, null=True,
                            verbose_name=_('Name'))
   
    parent = models.ForeignKey('AccountGroup', blank=True, null=True,
                               related_name='children')
   
    is_subgroup = models.BooleanField(default=False, editable=False)
       
    def save(self, *args, **kwargs):
        self.is_subgroup = False
       
        if self.parent is not None:
            self.is_subgroup = True
           
        return super(AccountGroup, self).save(*args, **kwargs)

I would like to be able to manually specify an order for AccountGroups in relation to their siblings, so that I can have a final result of something like the following pic (which describes a set of groups and accounts, with the latter being the rightmost, M000000000-format codes.

http://img200.imageshack.us/img200/9423/groupsandaccounts.png


As you can see from the picture, accounts can be located under any number of groups, and neither Accounts nor AccountGroups are not sorted naturally.

Short of using something like django-treebeard[3], how can I write a solution for this use case?

Thanks in advance for all your help!


Cheers,
André Terra


[1] http://dpaste.com/hold/571566/
[2] https://docs.djangoproject.com/en/dev/ref/models/fields/#commaseparatedintegerfield
[3] https://tabo.pe/projects/django-treebeard/docs/1.52/

Andre Terra

unread,
Jul 21, 2011, 8:45:56 AM7/21/11
to django...@googlegroups.com
Shameless bump.

Althought the original post is long, I assure you it's easy to understand.

Any suggestions?


Cheers,
André Terra

Daniel Roseman

unread,
Jul 21, 2011, 9:12:08 AM7/21/11
to django...@googlegroups.com
Well, a pre-order algorithm like MPTT - as provided by django-treebeard or django-mptt - *is* the way to go here. You don't say why you don't want to go down that route - what's your objection?
--
DR.

Andre Terra

unread,
Jul 21, 2011, 9:24:29 AM7/21/11
to django...@googlegroups.com
django-mptt looks great, I totally forgot it existed. It seems I visited the project's website ages ago, but I honestly don't recall it.

As for django-treebeard, I didn't like the API, docs and examples. Actually, the examples don't even work, so I was a little worried about using it.

I'll give mptt a whirl, thanks a billion.


Cheers,
AT

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/llCUKidcbscJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages