Understanding the difference between .get and .first in Django

Posted on Fri 21 May 2021 in Better Django

Photo

When retrieving a single record in Django, the ORM offers two possible methods: .get() and .first(). Let's learn how each one works and when to use them.

Get

Django's most basic way to retrieve a single record is the .get() method. It is used as follows:

burger = Menu.objects.get(name="Beef Burger")

Quick facts about .get():

  • It retrieves only one record.

  • If no record exists that meets the given criteria, it raises a DoesNotExist exception.

  • If more than one record with the given criteria exists, it raises a MultipleObjectsReturned exception.

First

A different way to get one record is …


Continue reading

Using Exceptions instead of Dynamic Typing

Posted on Mon 17 May 2021 in Better Python

Photo

Python's dynamic typing is great, right? You can just do whatever you want with variables. FREEDOM! ;-) This comes at a price though, and sometimes one we're not always aware of.

Trying not to fail

For years I took advantage of Python's dynamic typing by having functions return an error state via a different data type. For example:

def double(int_input):
    if not isinstance(int_input, int):
        return "An integer must be provided"

    return int_input * 2

And consuming this function would have looked like this:

doubled_int = double(user_input)
if isinstance(doubled_int, str):
    print("You did not provide an integer")
    return

print(f …

Continue reading

Write better Django views

Posted on Fri 12 March 2021 in Better Django

image

Should you use class-based views (CBV's) or function-based views (FBV's) in Django? It seems a lot of people are pushing CBV's, touting them to be the "standard" way of writing views in Django. But why do FBV's still exist then? Just for backward-compatibility? This is my highly opinionated "view" (see what I did there??) on this matter.

The topic is hotly debated. Why are some people so passionate about CBV's? CBVs are said to be better because they abstract a lot of boiler-plate code into base classes and mixins. This is true. As a very basic example, instead of writing …


Continue reading

Learn the subtle differences between Save and Update in Django

Posted on Fri 12 February 2021 in Better Django

To save data in Django, you normally use .save() on a model instance. However the ORM also provides a .update() method on queryset objects. So which one should you use? Let's take a look at each, and then decide which one to use in which situations.

Save

The .save() method is used to write a model instance to the database. It can be an existing record or even a new one. For an existing record, Django will run a SQL UPDATE statement on the database. For a new record, Django will run an INSERT. Before Django 3.0, it would …


Continue reading

Results - VS Code vs PyCharm

Posted on Tue 02 February 2021 in Misc

I asked Reddit about text editors vs IDEs. In particular, I asked VS Code users why they preferred it over IDEs like PyCharm, and then I asked PyCharm users why they preferred it over text editors like VS Code. The results were very interesting.

VSCode users: Why do you prefer it over PyCharm?

PyCharm users: Why do you prefer it over VSCode or other editors?

Results: VS Code

Among VS Code users, the following reasons were listed most:

image

Results: PyCharm

And among PyCharm users, the following reasons were most common:

image

Conclusion

Looking at the above, I can conclude a few …


Continue reading