Skip to content

Instantly share code, notes, and snippets.

View KimSoungRyoul's full-sized avatar
🤣

KimSoungRyoul KimSoungRyoul

🤣
View GitHub Profile
from datetime import datetime
from django.contrib.auth.models import User
from django.db import models
class Company(models.Model):
name: str = models.CharField(max_length=128, null=False)
tel_num: str = models.CharField(max_length=128, null=False)
address: str = models.CharField(max_length=128, null=False)
# app_name.management.commands.bulk_create.py를 만들고 이 내용을 bulk_create.py에 복붙하세요
from random import randint
from django.core.management import BaseCommand
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
from django.utils import timezone
python manage.py bulk_create --user_cnt 300 --company_cnt 100 --product_cnt 1000 --order_cnt 10000
@KimSoungRyoul
KimSoungRyoul / inner join 안하고 subQuery로도 하고싶다.sql
Created April 21, 2019 14:25
ORM에서 쿼리 전략을 변경하는 것이 가능할까?
SELECT *,
(select "descriptions"
from orm_practice_app_order
where "orm_practice_app_orderedproduct"."related_order_id" = "orm_practice_app_order"."id"
AND "descriptions" = '주문의 상세내용입니다...1') as descriptions
FROM "orm_practice_app_orderedproduct"
WHERE "orm_practice_app_orderedproduct"."id" = 1
@KimSoungRyoul
KimSoungRyoul / helle_queryset.py
Created May 6, 2019 05:14
queryset 기본 구조
(Model.objects
.filter(조건절)
.select_related('정방향_참조_필드') # 해당 필드를 join해서 가져온다.
.prefetch_related('역방향_참조_필드') # 해당 필드는 추가쿼리로 가져온다.
)
select * from 'Model' m
(inner OR left outer) join '정방향_참조_필드' r on m.r_id=r.id
'where '조건절';
select * from '역방향_참조_필드' where id in ('첫번째 쿼리 결과의 id 리스트');
@KimSoungRyoul
KimSoungRyoul / no_prefetch_related.py
Created May 6, 2019 06:25
Queryset께서는 prefetch_related를 허락하지 않으셨다
@KimSoungRyoul
KimSoungRyoul / yes_prefetch_related.py
Created May 6, 2019 07:59
의도한대로 추가쿼리로 가져온다
@KimSoungRyoul
KimSoungRyoul / 의도한쿼리.py
Last active September 13, 2019 14:00
내가 원했던 쿼리
SELECT `orm_practice_app_company`.`id`,
`orm_practice_app_company`.`name`,
`orm_practice_app_company`.`tel_num`,
`orm_practice_app_company`.`address`
FROM `orm_practice_app_company`
WHERE `orm_practice_app_company`.`name` = 'company_name1';
SELECT "orm_practice_app_product"."id", "orm_practice_app_product"."name", "orm_practice_app_product"."price", "orm_practice_app_product"."product_owned_company_id"
FROM "orm_practice_app_product"