Tag: django backend

  • Python dict.get() vs dict[‘brackets’], which approach is better?

    Python dict.get() vs dict[‘brackets’], which approach is better?

    by

    in

    Using the .get() method on a dictionary is generally considered a better approach than using the direct dictionary indexing dictionary[‘key’] method because the .get() method provides a default value if the key is not found in the dictionary, whereas direct dictionary indexing raises a KeyError exception. Here is an example: my_dict = {‘a’: 1, ‘b’:…

  • Why its better to use .exists() method on query rather then do try-except block?

    Why its better to use .exists() method on query rather then do try-except block?

    by

    in

    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?

    Where should I store database queries in Django?

    by

    in

    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…