0

I was wondering about best practices in Django of validating the tables content

I am creating a Sales Orders and my SO should check availability of the items I have in stock and if they are not in stock it will trigger manufacturing orders and purchase orders.

I don't want to make very complex view and looking for a way to decouple logic from there and also I predict performance issues.

What are best practices or ready solutions I can use in Django framework to address view complexity ?

I see different possibilities but I am wondering what will be the best fit in my case :

  • managers

  • celery - just to run a job occasionally I want the app to be real time so I don't like this option.

  • using signals /pre_save/post_sav

  • model validation

  • creating extra layer like services.py file

    Since I am new to Django I am a bit puzzled what root to take.

Community
  • 1
  • 1
Ilya Bibik
  • 3,924
  • 4
  • 23
  • 48

2 Answers2

2

Not sure if this is the answer you are looking for.

Signals are for doing things automatically when events happen. Most commonly used to do things before and after model operations. So if you need to do something every time you save a record or every time you create a new record or delete that is where you use signals.

Managers are used to manage record retrieval and manipulations. If you want to do some clever way of retrieving data you can define a custom manager and add some custom methods to it. If you want to override some default behaviors of querysets you would also do it with a custom manager.

Celery is for running things asynchronously. If you are worried that some processing you are doing might take a long time that is were you might consider offloading things to celery. A friendly warning though, doing things asynchronously raises complexity of your code quite a bit, since you need to add some mechanism to pass the data back from celery tasks into your django app and your users.

services.py link that you posted seems to do what you want, it just provides a place where you can put logic that is not specific to a particular view.

Mad Wombat
  • 14,490
  • 14
  • 73
  • 109
1

Here on stackoverflow, i got an advice from some experienced developers that premature optimization is the root of all evil.

What i suggest is keep it simple. Making the view a little more complex is actually better than effectively adding one more layer of complexity. I would suggest that you try to put most of you logic in models and whatever remains after that in views.

Also, unnecessarily using multiple packages would not solve much of your problem so use the when its necessary. Otherwise try to write the minimal logic yourself so that you donot have to use many apps.

Signals and other things as everybody say is not a great thing however promising it may seem. Just try to make things simpler.

One more point from my side as you are just starting out, go through class based views and try to use them when you get familiar. That will simplify your views the most. Plus, if ou are new to django, read a little code. https://github.com/vitorfs/bootcamp might help you in initiation.

sprksh
  • 2,204
  • 2
  • 26
  • 43