inspectdb ( db table column migration )

python manage.py inspectdb TableName > output.py

ORM

https://techblog.yogiyo.co.kr/django-queryset-1-14b0cc715eb7

Filter

Model.objects
  .filter(조건절)
  .select_related('정방향_참조_필드')   # 해당 필드를 join해서 가져온다.
  .prefetch_related('역방향_참조_필드') # 해당 필드는 추가 쿼리로 가져온다.

특정 칼럼 override 하기

class Scene(models.Model):
	rgb = models.TextField(blank=True, null=True)
	
	    @property
	    def get_rgb(self):
	        if self.red_channel == 1 and self.green_channel == 2 and self.blue_channel == 3 and self.alpha_channel == 0:
	            return "RGB"
	        elif self.red_channel == 1 and self.green_channel == 2 and self.blue_channel == 3 and self.alpha_channel == 4:
	            return "RGBN"
	        elif self.red_channel == 3 and self.green_channel == 2 and self.blue_channel == 1 and self.alpha_channel == 0:
	            return "BGR"
	        elif self.red_channel == 3 and self.green_channel == 2 and self.blue_channel == 1 and self.alpha_channel == 4:
	            return "BGRN"
	        else:
	            return ""
	
	    def save(self, *args, **kwargs):
	        self.rgb = self.get_rgb
	        super(Scene, self).save(*args, **kwargs)
	
	    class Meta:
	        managed = False
	        db_table = "tbl_sia_scenes"

없는 칼럼 사용하려면?

https://pypi.org/project/django-property-filter/

library 사용!

WGSI

WGSI

REST API

django REST API

swagger

filter_backends

@swagger_auto_schema(responses={200: "Scenes list"})
class SceneViewSet(viewsets.ModelViewSet):
    serializer_class = SceneSerializer
    filter_backends = [
        DjangoFilterBackend,
        OrderingFilter,
        SearchFilter,
    ]
    filterset_fields = ["id", "scene_name"]
    ordering_fields = "__all__"
    ordering = ["id"]
    search_fields = ["scene_name"]