Django and domain level

How to organize my domain layer using django?

I know that I can write user managers to store my requests, but what if I want something more flexible, like a specification template.

Are there any domain templates that are unique to Django?

I am currently developing a large application using Django, and I would like to know how to properly make the domain layer.

+3
source share
1 answer

This question is a bit subjective, but here are my two cents:

  • The business logic for changing multiple objects at the same time should go to the manager.
  • , MyModel.objects.create_complex(foo, bar)
  • - , .. ( ) , my_instance.get_accumulated_interest(). , .
  • - clean , clean . , .
  • , , , .
  • , - .

" X", , , Django alltogether. .

Edit:

" " , . Q , :

q = Q(field__isnull = False) | Q(otherfield__isnull = False)
objects = Model.objects.filter(q)

II:

: Python . . :

def HasFooBar(model_class):
    return list(model_class.objects.filter(somefield__contains = 'foobar'))

def BarHasBaz(model_class, arg):
    return list(model_class.objects.filter(somefield = arg))

objects = HasFooBar(MyModel) + BarHasBaz(OtherModel, baz)

, ?:)

+5

Source: https://habr.com/ru/post/1777277/


All Articles