-
[Python] Mutable vs Immutable, DB Key Types, Queryset vs ObjectTIL 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
'TIL' 카테고리의 다른 글
Web Dev Bootcamp TIL Day-46(Django Reverse Lookup) (0) 2022.06.17 Web Dev Bootcamp TIL Day-45(DRF CBV & Custom User Model) (0) 2022.06.17 Web Dev Bootcamp TIL Day-36(Django MySQL: many-many. query & csv -> json) (0) 2022.06.07 [Python] Arguments (0) 2022.06.06 Web Dev Bootcamp TIL Day-33 (Django proj. Start & Recommendation Systems ) (0) 2022.06.04 - mutable: