Python, Django
-
파이썬 딕셔너리 합치기 (dictionary)Python, Django 2021. 7. 8. 10:47
update 함수 a = update(b) 를 사용하면 b 딕셔너리의 key, value를 a 딕셔너리에 추가할 수 있습니다. 사용 예 a = {'a' : 1} b = {'b' : 2} a.update(b) // a = {'a': 1, 'b': 2} // b = {'b': 2} 주의 같은 key값이 있다면 덮어쓰여집니다. 함수의 정의 update(other) other의 key, value를 가지고 딕셔너리를 업데이트 한다. 같은 key가 있으면 덮어 쓰여진다. None 을 리턴한다. d.update(red=1, blue=2) 와 같이 keyword args가 주어지면 해당 key/value 값을 업데이트 한다.
-
Django Shell에서 자주쓰는 기능 10가지Python, Django 2021. 6. 3. 15:12
1. refresh_from_db 데이터베이스에서 값을 새로 가져오고 싶을 때 사용합니다. 아래 예제처럼, update를 하고 나서 데이터베이스에 값을 다시 확인하고 싶을 때 사용합니다. def test_update_result(self): obj = MyModel.objects.create(val=1) MyModel.objects.filter(pk=obj.pk).update(val=F('val') + 1) # At this point obj.val is still 1, but the value in the database # was updated to 2. The object's updated value needs to be reloaded # from the database. obj.refresh_f..
-
type vs. isinstancePython, Django 2021. 5. 26. 16:38
https://switowski.com/blog/type-vs-isinstance#:~:text=Difference%20between%20isinstance%20and%20type&text=type%20only%20returns%20the%20type,object%20(first%20parameter)%20is%3A&text=or%20an%20instance%20of%20a,specified%20as%20a%20second%20parameter.