👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I am writing a project in Django and I see that 80% of the code is in the file models.py
. This code is confusing and, after a certain time, I cease to understand what is really happening.
Here is what bothers me:
- I find it ugly that my model level (which was supposed to be responsible only for the work with data from a database) is also sending email, walking on API to other services, etc.
- Also, I find it unacceptable to place business logic in the view, because
this way it becomes difficult to control. For example, in my
application there are at least three ways to create new
instances of
User
, but technically it should create them uniformly. - I do not always notice when the methods and properties of my models become non-deterministic and when they develop side effects.
Here is a simple example. At first, the User
model was like this:
class User(db.Models):
def get_present_name(self):
return self.name or "Anonymous"
def activate(self):
self.status = "activated"
self.save()
Over time, it turned into this:
class User(db.Models):
def get_present_name(self):
# property became non-deterministic in terms of database
# data is taken from another service by api
return remote_api.request_user_name(self.uid) or "Anonymous"
def activate(self):
# method now has a side effect (send message to user)
self.status = "activated"
self.save()
send_mail("Your account is activated!", "…", [self.email])
What I want is to separate entities in my code:
- Entities of my database, persistence level: What data does my application keep?
- Entities of my application, business logic level: What does my application do?
What are the good practices to implement such an approach that can be applied in Django?
👻 Read also: what is the best laptop for engineering students?
Separation of business logic and data access in django __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Separation of business logic and data access in django __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Separation of business logic and data access in django, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Separation of business logic and data access in django
- Deutsch Separation of business logic and data access in django
- Français Separation of business logic and data access in django
- Español Separation of business logic and data access in django
- Türk Separation of business logic and data access in django
- Русский Separation of business logic and data access in django
- Português Separation of business logic and data access in django
- Polski Separation of business logic and data access in django
- Nederlandse Separation of business logic and data access in django
- 中文 Separation of business logic and data access in django
- 한국어 Separation of business logic and data access in django
- 日本語 Separation of business logic and data access in django
- हिन्दी Separation of business logic and data access in django
Tallinn | 2023-02-06
Mail PHP module is always a bit confusing 😭 Separation of business logic and data access in django is not the only problem I encountered. Will get back tomorrow with feedback
Vigrinia | 2023-02-06
Maybe there are another answers? What Separation of business logic and data access in django exactly means?. I am just not quite sure it is the best method
Paris | 2023-02-06
Simply put and clear. Thank you for sharing. Separation of business logic and data access in django and other issues with Python functions was always my weak point 😁. Will get back tomorrow with feedback