Tag: django models
-
Why its better to use .exists() method on query rather then do try-except block?
Both of the provided code samples achieve the same result, but they use different approaches to handle the case when the instance with the given primary key does not exist. class Balance(models.Model): current_balance = models.DecimalField(_(“Current Balance”), max_digits=12, decimal_places=2, default=0.00) last_balance = models.DecimalField(_(“Last Balance”), max_digits=12, decimal_places=2, null=True) def save(self, *args, **kwargs): if self.pk: try: prev_state =…
-
Where should I store database queries in Django?
The Django business logic dilemma? In general, it’s a good practice to keep your code organized and modular. If your view code includes complex database queries or large amounts of database-related logic, it might be a good idea to move those queries to a separate file, such as a Django model or manager. Here are…