1
0
mirror of https://github.com/Damillora/Altessimo synced 2024-11-24 06:37:33 +00:00
Altessimo/categories/models.py

43 lines
1.2 KiB
Python
Raw Normal View History

2020-12-15 21:32:20 +00:00
from django.db import models
2020-12-16 17:36:29 +00:00
from django.utils.text import slugify
2020-12-15 21:32:20 +00:00
2020-12-16 14:26:15 +00:00
class Branch(models.Model):
name = models.CharField(max_length=255,blank=True)
2020-12-16 17:36:29 +00:00
acronym = models.CharField(max_length=20)
description = models.TextField(blank=True)
logo = models.ImageField(upload_to="logo",blank=True)
2020-12-16 14:26:15 +00:00
def __str__(self):
return self.name+" ["+self.acronym+"]"
2020-12-15 21:32:20 +00:00
# Create your models here.
2020-12-16 07:29:08 +00:00
class CategoryManager(models.Manager):
2020-12-17 12:21:37 +00:00
def comma_to_qs(self, categories_str):
2020-12-16 07:29:08 +00:00
final_ids = []
if categories_str:
for category in categories_str.split(','):
obj, created = self.get_or_create(name=category.strip())
final_ids.append(obj.id)
qs = self.get_queryset().filter(id__in=final_ids).distinct()
return qs
2020-12-17 13:27:03 +00:00
return self.none()
2020-12-15 21:32:20 +00:00
class Category(models.Model):
name = models.CharField(max_length=255)
2020-12-16 17:36:29 +00:00
slug = models.SlugField(max_length=255)
2020-12-15 21:32:20 +00:00
description = models.TextField(blank=True)
2020-12-16 07:29:08 +00:00
objects = CategoryManager()
2020-12-15 21:32:20 +00:00
class Meta:
2020-12-16 08:51:48 +00:00
ordering = [ 'name' ]
2020-12-15 21:32:20 +00:00
verbose_name_plural = "Categories"
def __str__(self):
return self.name
2020-12-16 17:36:29 +00:00
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super().save(*args,**kwargs)