class Category(models.Model):
parent = models.ForeignKey('self', null=True)
title = models.CharField(...)
class Product(models.Model):
title = models.CharField(...)
parent = models.ManyToManyField('self', null=True)
# in class Category
is_best = models.Boolean(default=False)
def foo():
foo()
class Bar():
def foo(self, ...):
pass
class Foo(Bar):
...
def foo(self, ...):
super(Foo, self).foo(...)
pass
class Foo(models.Model):
...
pass
@receiver(post_save, sender=Foo)
def foo_bar(sender, instance, **kwargs):
instance.save()
pip freeze > requirements.txt
Django==1.10
django-cors-middleware==1.3.0
djangorestframework==3.4.5
djangorestframework-jsonapi==2.1.0
inflection==0.3.1
...
def handle_post_slug(sender, instance, *args, **kwargs):
from pyutils import translit
slug = translit(instance.title)
if Post.objects.filter(slug=slug).exclude(id=instance.id).exists():
slug = "-".join([slug, str(instance.id)])
instance.slug = slug
pre_save.connect(handle_post_slug, sender=Post)
class Article(models.Model):
...
case = models.ForeignKey(Case, related_name='articles')
...
class CaseSerializer(serializers.ModelSerializer):
class Meta:
model = Case
fields = (..., ..., 'articles')
class ProductViewSet(viewsets.ReadOnlyModelViewSet):
# queryset = Product.objects.filter(is_active=True)
def get_queryset(self):
queryset = Product.objects.filter(is_active=True)
attributes = self.request.query_params.get('attributes', None)
# if attributes is not None:
# custom logic of search
# list_of_ids = [...]
# queryset.filter(id__in=list_of_ids)
return queryset