TIL

[Python] Mutable vs Immutable, DB Key Types, Queryset vs Object

frannyk 2022. 6. 15. 15:13

Mutable vs Immutable Objects

Whenever an object is instantiated, it is assigned a unique object id. The type of the object is defined at the runtime and it can’t be changed afterwards. However, it’s state can be changed if it is a mutable object.

  • mutable:
    • list, dictionary
  • immutable:
    • int, float, tuple, boolean, string

DB Field Key Types

  • Parent Key(PK)
    • Used to serve as a unique identifier for each row in a table
    • Cannot accept NULL values
    • Creates clustered index
    • A Primary key supports auto increment value
  • Unique Key(UK)
    • Uniquely determines a row which isn’t primary key
    • Can accepts NULL values
    • Creates non-clustered index
    • A unique key does not supports auto increment value
  • Foreign Key(FK)
    • Field that refers to the PK in another table
    • There can be multiple FKs in one table

Django Filter() vs Get()

  • Filter()
    • Returns a queryset object
    • If no item was found matching your criteria, filter() returns an empty queryset instead of an error
  • Get()
    • You expect one and only one item that matches your criteria
    • Returns an error if the item does not exist or if multiple items exist that match criteria
    • Utilize try/except block or shortcut functions such as get_object_or_404 in order to handle exceptions