Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested categories #57

Open
mgazzin opened this issue Nov 7, 2022 · 0 comments
Open

nested categories #57

mgazzin opened this issue Nov 7, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@mgazzin
Copy link

mgazzin commented Nov 7, 2022

I am using in django the following models.py:

class Expense(models.Model):
    name = models.CharField(max_length=50)
    date = models.DateField(unique=False, blank=False)
    slug = models.SlugField(unique=True, null=True, default='')
    price = models.DecimalField(default=0.0, blank=True, max_digits = 20, decimal_places = 2)
    category    = models.ForeignKey(
        'Category',
        related_name="Expense",
        on_delete=models.CASCADE
    )
    account = models.ForeignKey(Account, on_delete=models.CASCADE, verbose_name=u"Account", help_text=u"account")

def __str__(self):
    return '{},{},{}'.format(self.name, self.date, self.price)

def save(self, *args, **kwargs):
    self.slug = slugify(self.name)
    super(Expense,self).save(*args, **kwargs)

def get_absolute_url(self):
    return self.slug

class Category(MPTTModel):
    name = models.CharField(max_length=200)
    slug = models.SlugField(unique=True, null=True, default='')
    parent = TreeForeignKey(
        'self',
        blank=True,
        null=True,
        related_name='child',
        on_delete=models.CASCADE
    )
class Meta:
    unique_together = ('slug', 'parent',)
    verbose_name_plural = "categories"

def __str__(self):
    full_path = [self.name]
    k = self.parent
    while k is not None:
        full_path.append(k.name)
        k = k.parent

    return ' -> '.join(full_path[::-1])

The TreeForeignKey allows me to define nested categories, such as Home -> Electricity and so on.
I am using the following Slick Report view.py:

class TotalExpenses(SlickReportView):

report_model = Expense
date_field = 'date'
group_by = 'category'
columns = ['name', SlickReportField.create(method=Sum, field='price', name='price__sum', verbose_name=('Total category $'))]

charts_settings = [
 {
    'type': 'bar',
    'data_source': 'price__sum',
    'title_source': 'name',
 },
]

It works but I would like to sum only level 1 categories. Do you know how this might be possible?

@RamezIssac RamezIssac added the enhancement New feature or request label May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants