Django

파이썬 장고 실무 심화 4주차_SerializerMethodField, StringRelatedField

끈끈 2023. 4. 21. 12:36

 

How to manage static files

 

pip install Pillow

 

https://docs.djangoproject.com/en/4.2/howto/static-files/

 

Django

The web framework for perfectionists with deadlines.

docs.djangoproject.com

 


 

Serializer의 커스텀 가능

 

articles > serializers.py:

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = "__all__"


class ArticleListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ("pk", "title", "image", "updated_at", "user")

 

SerializerMethodField

 

https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

 

Serializer fields - Django REST framework

 

www.django-rest-framework.org

 


 

access token도 Environments에 저장 가능

 

access token
{{token}}
form-data로 보내기

 


 

StringRelatedField

 

ForeignKey로 연결된 모델의 __str__ 메서드에서 정의한 string을 리턴.

 

many=True, read_only=True 등을 가질 수 있다

 

https://www.django-rest-framework.org/api-guide/relations/

 

Serializer relations - Django REST framework

relations.py Data structures, not algorithms, are central to programming. — Rob Pike Relational fields are used to represent model relationships. They can be applied to ForeignKey, ManyToManyField and OneToOneField relationships, as well as to reverse re

www.django-rest-framework.org

 

Nested relationships

 

serializer 자체를 serializer 필드로 사용하는 방법

 

리스트로 받고 싶을 때 (many=True)

 

Nested relationships

 

https://www.django-rest-framework.org/api-guide/relations/#nested-relationships

 

Serializer relations - Django REST framework

relations.py Data structures, not algorithms, are central to programming. — Rob Pike Relational fields are used to represent model relationships. They can be applied to ForeignKey, ManyToManyField and OneToOneField relationships, as well as to reverse re

www.django-rest-framework.org

 


 

Q objects

 

쿼리에 조건들을 걸 때 Q를 사용할 수 있다

 

  • | : or
  • ^ : and

 

from django.db.models import Q

 

https://docs.djangoproject.com/en/4.2/topics/db/queries/#complex-lookups-with-q-objects

 

Django

The web framework for perfectionists with deadlines.

docs.djangoproject.com