Add automatic category sorting
This commit is contained in:
parent
ea9d871e25
commit
c91994d041
4 changed files with 56 additions and 4 deletions
|
@ -6,10 +6,21 @@ from django.forms import ModelForm
|
|||
class Category(models.Model):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
name = models.CharField(max_length=64)
|
||||
full_name = models.CharField(max_length=512, editable=False, default="")
|
||||
parent = models.ForeignKey("self", on_delete=models.SET_NULL, blank=True, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
if self.parent is None: return self.name
|
||||
return f"{self.parent}>{self.name}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.full_name = str(self)
|
||||
super().save(*args, **kwargs)
|
||||
for child in self.__class__.objects.filter(parent=self):
|
||||
child.save()
|
||||
|
||||
class Meta:
|
||||
ordering = ['full_name']
|
||||
|
||||
|
||||
class Transaction(models.Model):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue