Django-tastypie: SHOW 및 INDEX μž‘μ—…μ— λŒ€ν•œ λ‹€λ₯Έ ν•„λ“œ

에 λ§Œλ“  2010λ…„ 10μ›” 25일  Β·  40μ½”λ©˜νŠΈ  Β·  좜처: django-tastypie/django-tastypie

μ•ˆλ…•ν•˜μ„Έμš”, μž‘μ—…μ— 따라 λ¦¬μ†ŒμŠ€μ— λŒ€ν•΄ λ‹€λ₯Έ ν•„λ“œλ₯Ό ν‘œμ‹œν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

λ‚΄κ°€ μ›ν•˜λŠ” μ΄μœ λŠ” λ¦¬μ†ŒμŠ€μ— ν¬ν•¨λœ λͺ¨λ“  has_many ν•„λ“œλ‘œ 인덱슀 μž‘μ—…μ˜ 뢀담을 ν”Όν•˜κΈ° μœ„ν•¨μž…λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ μ €μž 색인 λ³΄κΈ°μ—μ„œλŠ” 그의 μ΄λ¦„λ§Œ λ°˜ν™˜ν•˜κ³  μ‹Άμ§€λ§Œ ν‘œμ‹œ μž‘μ—…μ˜ 경우 λͺ¨λ“  μ±… λ¦¬μ†ŒμŠ€λ₯Ό ν¬ν•¨ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

감사 ν•΄μš”,
보얀

documentation feature

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ‚΄κ°€ μ‚¬μš©ν•˜λŠ” κ°„λ‹¨ν•œ ν•΄κ²° 방법은 μ΄λŸ¬ν•œ ν•„λ“œλ₯Ό νŽΈμ§‘ν•˜κΈ° μœ„ν•΄ get_detail & get_list λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.
μ΄λ ‡κ²Œ ν•˜λ©΄ μ‹€μ œλ‘œ ν•„λ“œμ— λŒ€ν•œ 데이터λ₯Ό κ°€μ Έμ˜¨ λ‹€μŒ λ²ˆλ“€μ—μ„œ μ‚­μ œν•˜λŠ” μ˜€λ²„ν—€λ“œκ°€ μ ˆμ•½λ˜μ§€λ§Œ 이 λ©”μ„œλ“œκ°€ μŠ€λ ˆλ“œλ‘œλΆ€ν„° μ•ˆμ „ν•œμ§€λŠ” ν™•μ‹€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ¦¬μ†ŒμŠ€ κ°œμ²΄κ°€ λͺ¨λ“  API ν˜ΈμΆœμ—μ„œ μƒμ„±λ˜μ§€ μ•ŠλŠ” κ²ƒμ²˜λŸΌ 보이기 λ•Œλ¬Έμž…λ‹ˆλ‹€.
λˆ„κ΅°κ°€κ°€ 이것에 λŒ€ν•΄ 논평할 수 μžˆλ‹€λ©΄ 쒋을 κ²ƒμž…λ‹ˆλ‹€.

μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

class ArticleResource(BaseModelResource):
    owner = fields.ToOneField(UserResource, 'owner', full=True)

    class Meta:
        resource_name = "articles"

    def get_list(self, request, **kwargs):
        self.fields.pop("comments", None)
        return super(ArticleResource, self).get_list(request, **kwargs)

    def get_detail(self, request, **kwargs):
        self.fields["comments"] = fields.ToManyField(CommentResource, 'comments', full=True)
        return super(ArticleResource, self).get_detail(request, **kwargs)

λͺ¨λ“  40 λŒ“κΈ€

+1
λ‚˜λŠ” 이 κΈ°λŠ₯을 κ°„μ ˆνžˆ 바라고 μžˆλ‹€: "detail field"와 "list field"λŠ” 뢄리될 수 μžˆλ‹€

+1(#44μ—μ„œ 볡제λ₯Ό 톡해)

인덱슀 μž‘μ—…μ—μ„œ λ°μ΄ν„°μ˜ ν•˜μœ„ 집합을 더 μ‰½κ²Œ κ°€μ Έμ˜¬ 수 μžˆλŠ” 두 가지가 μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

  1. full_dehydrateκ°€ index 및 get λ©”μ„œλ“œμ— λŒ€ν•΄ λ‹€λ₯Έ ν•„λ“œλ₯Ό μ‚¬μš©ν•˜λ„λ‘ ν•©λ‹ˆλ‹€. μ•„λ§ˆλ„ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

메타:
index_exclude_fields = ['some_m2m_field']

μ΄λ ‡κ²Œ ν•˜λ©΄ 일뢀 db 쿼리λ₯Ό μ €μž₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  1. νƒˆμˆ˜λ₯Ό μœ„ν•΄ μΆ”κ°€ '방법' λ§€κ°œλ³€μˆ˜λ₯Ό μ „λ‹¬ν•˜μ‹­μ‹œμ˜€. 이λ₯Ό 톡해 μΈλ±μŠ€μ— ν•„μš”ν•˜μ§€ μ•Šμ€ 일뢀 ν•„λ“œλ₯Ό κ±΄λ„ˆλ›°λŠ” 것과 같이 λ²ˆλ“€μ„ μ‚¬μš©μž μ •μ˜ν•  수 있으며 일뢀 λŒ€μ—­ν­μ„ μ ˆμ•½ν•˜κ³ μž ν•©λ‹ˆλ‹€.

λ‚˜λŠ” 1. 캐싱에 영ν–₯을 λ―ΈμΉ  것이라고 ν‰κ°€ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.
μ–΄λ–»κ²Œ μƒκ°ν•˜λ‚˜μš”?

λ˜ν•œ #48μ—μ„œ μ†μ•˜μ§€λ§Œ 각 λ¬Έμ œμ—λŠ” κ°€μΉ˜ μžˆλŠ” 츑면이 μžˆμŠ΅λ‹ˆλ‹€.

κ°€λŠ₯ν•˜λ©΄ 이 κΈ°λŠ₯을 ν•΄μ œν•˜κ³  μ‹Άμ—ˆμ§€λ§Œ 총λ ₯이 λΆ€μ‘±ν–ˆμŠ΅λ‹ˆλ‹€. μ–΄μ¨Œλ“  더 λ‚˜μ€ 파일 지원을 μœ„ν•΄ 거기에 μžˆμ–΄μ•Ό ν•˜λ―€λ‘œ ν•΄κ²°ν•΄μ•Ό ν•©λ‹ˆλ‹€. 이에 λŒ€ν•΄ 1.0을 λŒ€μƒμœΌλ‘œ ν•©λ‹ˆλ‹€.

이에 λŒ€ν•œ λ‚΄ μ‚¬μš© 사둀λ₯Ό 쑰금 더 μ‘°μ‚¬ν•œ ν›„ 보기/λͺ©λ‘ 보기보닀 더 μœ μ—°ν•œ κ²ƒμœΌλ‘œ κ΅¬ν˜„ν•˜λ„λ‘ μ œμ•ˆν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€. λ‚΄κ°€ μ •λ§λ‘œ ν•  수 μžˆμ–΄μ•Ό ν•˜λŠ” 것은 λ‹€μŒκ³Ό 같은 쿼리슀트링 ν”Œλž˜κ·Έλ₯Ό μ·¨ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

&shape=[전체|λ‹¨μˆœ|μ—†μŒ]

좜λ ₯이 μ‚¬μš©μžκ°€ μ„ νƒν•œ μ„ΈλΆ€ μˆ˜μ€€μ„ λ°˜μ˜ν•˜λ„λ‘ ν•©λ‹ˆλ‹€. 이λ₯Ό ν•΄ν‚Ήν•  방법을 μ°Ύμ•˜μ§€λ§Œ ModelResource ν΄λž˜μŠ€κ°€ 생성될 λ•Œ ν•„λ“œκ°€ 포함/μ œμ™Έλ˜κΈ° λ•Œλ¬Έμ— λ‚˜μ€‘μ— 응닡 μ£ΌκΈ°μ—μ„œ μ‚¬μš©ν•  수 μžˆλŠ” 것을 λ³€κ²½ν•  수 μžˆλŠ” 방법을 찾지 λͺ»ν–ˆμŠ΅λ‹ˆλ‹€. λ”μš±μ΄ μ΄λŸ¬ν•œ 결정을 λ‚΄λ¦¬κ³ μž ν•˜λŠ” ν”„λ‘œμ„ΈμŠ€μ˜ λ‹¨κ³„μ—μ„œ μš”μ²­ 개체λ₯Ό μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€.

+1

이 λ¬Έμ œμ™€ 관련이 μžˆλŠ” 것 κ°™κΈ° λ•Œλ¬Έμ— μ—¬κΈ°μ—μ„œ λ¨Όμ € 이 생각을 μ–ΈκΈ‰ν•˜κ² μŠ΅λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 자체적으둜 뢄리해야 ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

λ‹€λ₯Έ ν•„λ“œλ₯Ό ν‘œμ‹œν•˜λŠ” λ©”μ»€λ‹ˆμ¦˜μ΄ 개발되면 인증과도 톡합될 수 μžˆλ‹€λ©΄ μœ μš©ν•  κ²ƒμž…λ‹ˆλ‹€. 더 λ§Žκ±°λ‚˜ 적은 ν•„λ“œλ₯Ό λ…ΈμΆœν•  수 μžˆλ‹€λŠ” 것은 μ‚¬μš©μžμ˜ κΆŒν•œμ— 따라 맀우 κ°•λ ₯ν•  κ²ƒμž…λ‹ˆλ‹€. 이것이 이미 κ°€λŠ₯ν•˜λ‹€λ©΄ 그에 λŒ€ν•œ 언급을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.

+1

+1

+1

+1

+1

+1

+1

λͺ©λ‘/μ„ΈλΆ€ 정보 외에도 λ©”μ„œλ“œ(POST, PUT, GET)λ³„λ‘œ ν‘œμ‹œλ˜λŠ” ν•„λ“œ(ν•„λ“œ λ˜λŠ” μ œμ™Έ μ‚¬μš©)λ₯Ό μ œμ–΄ν•  수 있기λ₯Ό κΈ°λŒ€ν•©λ‹ˆλ‹€.

ν•΄κ²° 방법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. μ•„λž˜ 예λ₯Ό μ°Έμ‘°ν•˜μ„Έμš”. 원본 λͺ¨λΈμ˜ λ¦¬μ†ŒμŠ€ λͺ¨λΈ(μ˜ˆμ œμ—μ„œλŠ” UserResource)μ—μ„œ 아무 것도 μ œμ™Έν•˜μ§€ μ•ŠμœΌλ―€λ‘œ 인덱슀 λ³΄κΈ°μ—μ„œ 가득 차게 λ©λ‹ˆλ‹€. ν•˜μœ„ λͺ¨λΈμ„ ν¬ν•¨ν•˜λŠ” modelresource의 νƒˆμˆ˜ 방법(BlogPostResourceμ—λŠ” μž‘μ„±μžκ°€ 포함됨)으둜 μ΄λ™ν•˜κ³  λ²ˆλ“€μ˜ μš”μ†Œλ§Œ μ‚­μ œν•΄μ•Ό ν•©λ‹ˆλ‹€.

μ˜ˆμ‹œ:

class BlogPostResource(ModelResource):
     author = fields.ForeignKey(UserResource, 'author', full=True)
     ....
     class Meta:
         ...

def dehydrate(self, bundle):
         del bundle.data['author'].data['field_you_dont_wanna_show_here']
         del bundle.data['author'].data['field_you_dont_wanna_show_here']
         return bundle

λ”°λΌμ„œ μ‚¬μš©μžλ₯Ό λ‚˜μ—΄ν•˜λ €λŠ” 경우 μ—¬μ „νžˆ λͺ¨λ“  ν•„λ“œλ₯Ό κ°€μ Έμ˜€μ§€λ§Œ λΈ”λ‘œκ·Έ κ²Œμ‹œλ¬Όμ„ λ‚˜μ—΄ν•  λ•Œ 예λ₯Ό λ“€μ–΄ μž‘μ„±μžμ˜ 이름과 성을 κ°€μ Έμ˜¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

μ–΄λ–»κ²Œ μƒκ°ν•˜λ‚˜μš”?

맀우 λ”λŸ¬μš΄ ν•΄κ²° λ°©λ²•μ˜ 예: list_view에 ν”„λ‘œμ νŠΈ ν•„λ“œλ₯Ό ν‘œμ‹œν•˜μ§€ μ•Šμ§€λ§Œ λ¦¬μ†ŒμŠ€ μžμ²΄μ— μ•‘μ„ΈμŠ€ν•  ν•„μš” 없이 detail_viewμ—μ„œλŠ” ν‘œμ‹œλ©λ‹ˆλ‹€. ν•˜λ“œμ½”λ”© 없이 URL 자체λ₯Ό κ°€μ Έμ˜€λŠ” 것은 κ°€λŠ₯ν•΄μ•Ό ν•˜μ§€λ§Œ 확인할 μ‹œκ°„μ΄ μ—†μ—ˆμŠ΅λ‹ˆλ‹€. :

class CompanyResource(ModelResource):
       """
       Tastypie resource for Company
      """
       projects = fields.ToManyField('api.resources.ProjectResource',
                                  'projects',full=True)
       class Meta:
           queryset = Company.objects.all()
           resource_name = 'companies'

       def dehydrate(self, bundle):
           if bundle.request.path == "/api/v1/companies/":
               del bundle.data['projects']
           return bundle

+1

+1

+1

λ‚˜λŠ” λ˜ν•œ κΈ°μ°¨λ₯Ό 타면 쒋겠지 만

    def dehydrate(self, bundle):
        if self.get_resource_uri(bundle) == bundle.request.path:
            print "Detail"

        if self.get_resource_uri(bundle) != bundle.request.path:
            print "Not Detail - Could be list or reverse relationship."

        return bundle

κ·Έλž˜μ„œ λ‚˜λŠ” 이것에 λŒ€ν•΄ μ’€ 더 μƒκ°ν•˜κ³  @bmihelac 이 μ°Ύκ³  μžˆλ‹€κ³  μƒκ°ν•˜λŠ” 일을 ν•  수 μžˆλ„λ‘ ν•˜λŠ” 데 거의 κ·Όμ ‘ν•œ 무언가λ₯Ό 생각해

제곡된 @ashwoods 예제λ₯Ό μ‚¬μš©ν•˜μ—¬ μ„ΈλΆ€ 응닡인 κ²½μš°μ—λ§Œ ν”„λ‘œμ νŠΈ ν•„λ“œλ₯Ό ν‘œμ‹œν•˜λ €κ³  ν•œλ‹€κ³  κ°€μ •ν•©λ‹ˆλ‹€.

class CompanyResource(ModelResource):
    """
    Tastypie resource for Company
    """

    class Meta:
        queryset = Company.objects.all()
        resource_name = 'companies'
        additional_detail_fields = {'projects': fields.ToManyField('api.resources.ProjectResource', 'projects',full=True)}

    def dehydrate(self, bundle):
        # detect if detail
        if self.get_resource_uri(bundle) == bundle.request.path:
            # detail detected, include additional fields
            bundle = self.detail_dehydrate(bundle)

        return bundle

    # detail_dehydrate is basically full_dehydrate
    # except we'll loop over the additional_detail_fields
    # and we won't want to do the dehydrate(bundle) at the end
    def detail_dehydrate(self, bundle):
        """
        Given a bundle with an object instance, extract the information from it
        to populate the resource.
        """
        # Dehydrate each field.
        # loop over additional_detail_fields instead
        #for field_name, field_object in self.fields.items():
        for field_name, field_object in self._meta.additional_detail_fields.items():
            # A touch leaky but it makes URI resolution work.
            if getattr(field_object, 'dehydrated_type', None) == 'related':
                field_object.api_name = self._meta.api_name
                field_object.resource_name = self._meta.resource_name

            bundle.data[field_name] = field_object.dehydrate(bundle)

            # Check for an optional method to do further dehydration.
            method = getattr(self, "dehydrate_%s" % field_name, None)

            if method:
                bundle.data[field_name] = method(bundle)

        # dehydrating the bundle will create an infinite loop
        #bundle = self.dehydrate(bundle)
        return bundle

+1, μ§€κΈˆμ€ @dericcrago의 μˆ˜μ • 사항을 μ‚¬μš©ν•©λ‹ˆλ‹€.

+1

+1

# 526의 λΆ€λΆ„ κ΅¬ν˜„, λͺ¨λ“  것이 판맀되고 μžˆλŠ”μ§€ ν™•μ‹€ν•˜μ§€ μ•ŠμœΌλ©° ν…ŒμŠ€νŠΈ/λ¬Έμ„œκ°€ λΆ€μ‘±ν•©λ‹ˆλ‹€.

방금 이 티켓을 λ³΄μ•˜κ³ ... λ˜ν•œ μœ„μ˜ onyxfishκ°€ μ–ΈκΈ‰ν•œ 'λͺ¨μ–‘' μ ‘κ·Ό 방식과 μœ μ‚¬ν•©λ‹ˆλ‹€.

μ‚¬λžŒλ“€μ΄ λ‹€λ₯Έ κ²½μš°μ— λ‹€λ₯Έ 'λͺ¨μ–‘'을 원할 경우λ₯Ό λŒ€λΉ„ν•˜μ—¬ #526의 μ†”λ£¨μ…˜μ΄ μ•½κ°„ μ œν•œμ μ΄λΌκ³  μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€...

νƒˆμˆ˜ ν›„ ν•„λ“œλ₯Ό μ œκ±°ν•˜λΌλŠ” μ œμ•ˆμ— ... λ‚΄ λͺ¨λ“  μ΄μœ λŠ” μ²˜μŒμ— 값을 κ³„μ‚°ν•˜μ§€ μ•ŠλŠ” κ²ƒμž…λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ μ‘°κ±΄λΆ€λ‘œ 더 λ§Žμ€ μ„ΈλΆ€ 정보λ₯Ό μΆ”κ°€ν•  수 μžˆλ„λ‘ ν•˜λŠ” detail_dehydrate 후크에 λŒ€ν•œ 아이디어가 λ§ˆμŒμ— λ“­λ‹ˆλ‹€.

ν…ŒμŠ€νŠΈμ™€ λ¬Έμ„œλ₯Ό ν¬ν•¨ν•˜μ—¬ 두 가지 κ°€λŠ₯ν•œ κ΅¬ν˜„μ΄ κ°€λŠ₯ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€. #569에 ν•˜λ‚˜λ₯Ό μž‘μ„±ν–ˆκ³  #538도 μœ μ‚¬ν•œ κΈ°λŠ₯을 μˆ˜ν–‰ν•©λ‹ˆλ‹€(#538은 use_in κ°€ 호좜 κ°€λŠ₯ν•˜λ―€λ‘œ μ’€ 더 μœ μ—°ν•˜κ²Œ ν—ˆμš©ν•©λ‹ˆλ‹€). λ‚΄ κ΅¬ν˜„μ€ 이 κΈ°λŠ₯을 μ œμ–΄ν•˜κΈ° μœ„ν•΄ meta 속성을 μΆ”κ°€ν•˜λŠ” 반면(ν˜„μž¬ fields 속성과 μΌμΉ˜ν•¨) #538은 ν•„λ“œμ— 속성을 μΆ”κ°€ν•©λ‹ˆλ‹€. λ‘˜ λ‹€ μœ νš¨ν•œ κ²ƒμœΌλ‘œ 보이며 μ–΄λ–€ λ°©ν–₯으둜 갈 것인지에 λŒ€ν•œ 섀계 결정일 λΏμž…λ‹ˆλ‹€. 일뢀 ν•„λ“œλŠ” μžλ™μœΌλ‘œ 생성될 수 있고 μ΄ˆκΈ°ν™” λ§€κ°œλ³€μˆ˜λ₯Ό μˆ˜μ •ν•˜λŠ” 것이 λΆˆκ°€λŠ₯ν•  수 μžˆλ‹€λŠ” 점을 κ°μ•ˆν•  λ•Œ 메타에 μΆ”κ°€ν•˜λŠ” 것이 일관성 μžˆλŠ” κ²ƒμœΌλ‘œ 보이며 μ‚¬μš©ν•˜κΈ° 더 μ‰½μŠ΅λ‹ˆλ‹€. 또 λ‹€λ₯Έ λŒ€μ•ˆμ€ 두 pull μš”μ²­μ„ κ²°ν•©ν•˜κ³  meta 속성을 기반으둜 use_in λ§€κ°œλ³€μˆ˜κ°€ μžλ™μœΌλ‘œ μ„€μ •λ˜λ„λ‘ ν—ˆμš©ν•˜λŠ” κ²ƒμ΄μ§€λ§Œ, μ΄λŠ” API에 ν•„μš”ν•œ 것보닀 더 λ³΅μž‘ν•¨μ„ μΆ”κ°€ν•˜λŠ” κ²ƒμœΌλ‘œ λ³΄μž…λ‹ˆλ‹€. κ΄€λ ¨ pull μš”μ²­μ„ 지적해 μ£Όμ‹  @issackelly μ—κ²Œ κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€.

[#538의 μ›λž˜ 원인은 λ‚΄ #526을 μ •λ¦¬ν•˜λŠ” κ²ƒμ΄λ―€λ‘œ μ†Œλ¦¬λ₯Ό 지λ₯΄μ‹­μ‹œμ˜€.]
λ§Žμ€ μ˜λ―Έκ°€ μžˆμŠ΅λ‹ˆλ‹€ ... Meta μ ‘κ·Ό 방식은 μ‹€μ œλ‘œ ModelResource 등에 λŒ€ν•œ μ œμ™Έ λͺ©λ‘μ„ μ‚¬μš©ν•˜μ—¬ 저리λ₯Ό λ§Œλ“€ κ²ƒμž…λ‹ˆλ‹€ ...

λ‹€λ₯Έ ν‹°μΌ“μ—μ„œ λ§ν–ˆλ“―μ΄, 이와 같은 "λ‹¨μˆœν•œ" μ†”λ£¨μ…˜μ€ IMHO, 1.0 λ¦΄λ¦¬μŠ€μ— μΆ©λΆ„ν•©λ‹ˆλ‹€... "ν΄λΌμ΄μ–ΈνŠΈ 선택 κ°€λŠ₯ν•œ 'λͺ¨μ–‘'"κ³Ό 같은 더 λ³΅μž‘ν•œ μ†”λ£¨μ…˜μ€ 이후 λ¦΄λ¦¬μŠ€μ— λ°”λžŒμ§ν•  수 μžˆμŠ΅λ‹ˆλ‹€. .

@funkybob ν™•μ‹€νžˆ 더 λ³΅μž‘ν•œ μ†”λ£¨μ…˜μ΄ ν΄λΌμ΄μ–ΈνŠΈ μΈ‘μ—μ„œ 도움이 될 κ²ƒμ΄λΌλŠ” 데 λ™μ˜ν•˜μ§€λ§Œ 1.0이 릴리슀되기 전에 μ‚¬μš©ν•  수 μžˆλ„λ‘ 이 κΈ°λŠ₯을 μ΅œλŒ€ν•œ 빨리 ν¬ν•¨ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.

μ‹€μ œλ‘œ ν”„λ‘œλ•μ…˜ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ PR을 μ‚¬μš©ν•˜λ©΄μ„œ #538μ—μ„œ μ œκ³΅ν•˜λŠ” 콜백의 μœ μ—°μ„±μ΄ 정말 λ§ˆμŒμ— λ“­λ‹ˆλ‹€. κΆŒν•œμ— 따라 λŸ°νƒ€μž„μ— λ¦¬μ†ŒμŠ€λ₯Ό μˆ¨κ²¨μ•Ό ν•˜λŠ” λͺ‡ 가지 μ‚¬μš© 사둀가 μžˆμŠ΅λ‹ˆλ‹€.

이것은 # 569λ₯Ό μ‚¬μš©ν•˜μ—¬ λΆˆκ°€λŠ₯ν•  κ²ƒμž…λ‹ˆλ‹€.

μ•ˆλ…•ν•˜μ„Έμš”, μ–΄λ–€ μ†Œμ‹μ΄ μžˆμŠ΅λ‹ˆκΉŒ? κ·Έ PR은 삢을 훨씬 더 μ‰½κ²Œ λ§Œλ“­λ‹ˆλ‹€. κ°μ‚¬ν•©λ‹ˆλ‹€!

@dericcragoμ—μ„œ μ œκ³΅ν•˜λŠ” ν•΄ν‚ΉμœΌλ‘œ 진행

+1

λ‚΄κ°€ μ‚¬μš©ν•˜λŠ” κ°„λ‹¨ν•œ ν•΄κ²° 방법은 μ΄λŸ¬ν•œ ν•„λ“œλ₯Ό νŽΈμ§‘ν•˜κΈ° μœ„ν•΄ get_detail & get_list λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.
μ΄λ ‡κ²Œ ν•˜λ©΄ μ‹€μ œλ‘œ ν•„λ“œμ— λŒ€ν•œ 데이터λ₯Ό κ°€μ Έμ˜¨ λ‹€μŒ λ²ˆλ“€μ—μ„œ μ‚­μ œν•˜λŠ” μ˜€λ²„ν—€λ“œκ°€ μ ˆμ•½λ˜μ§€λ§Œ 이 λ©”μ„œλ“œκ°€ μŠ€λ ˆλ“œλ‘œλΆ€ν„° μ•ˆμ „ν•œμ§€λŠ” ν™•μ‹€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ¦¬μ†ŒμŠ€ κ°œμ²΄κ°€ λͺ¨λ“  API ν˜ΈμΆœμ—μ„œ μƒμ„±λ˜μ§€ μ•ŠλŠ” κ²ƒμ²˜λŸΌ 보이기 λ•Œλ¬Έμž…λ‹ˆλ‹€.
λˆ„κ΅°κ°€κ°€ 이것에 λŒ€ν•΄ 논평할 수 μžˆλ‹€λ©΄ 쒋을 κ²ƒμž…λ‹ˆλ‹€.

μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

class ArticleResource(BaseModelResource):
    owner = fields.ToOneField(UserResource, 'owner', full=True)

    class Meta:
        resource_name = "articles"

    def get_list(self, request, **kwargs):
        self.fields.pop("comments", None)
        return super(ArticleResource, self).get_list(request, **kwargs)

    def get_detail(self, request, **kwargs):
        self.fields["comments"] = fields.ToManyField(CommentResource, 'comments', full=True)
        return super(ArticleResource, self).get_detail(request, **kwargs)

+1

또 λ‹€λ₯Έ ν•΄κ²° 방법은 μ„ΈλΆ€ 정보 및 λͺ©λ‘ 보기에 λŒ€ν•΄ λ‹€λ₯Έ λ¦¬μ†ŒμŠ€λ₯Ό μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

from tastypie.resources import ModelResource
from django.contrib.auth.models import User

# detail will show everything except password
class UserResourceDetail(ModelResource):
    class Meta:
        queryset = User.objects.all()
    excludes = ('password',)
    resource_name = 'user'

# list will only show username & date_joined (and exclude password)
class UserResource(UserResourceDetail):
    class Meta(UserResourceDetail.Meta):
        fields = ('username', 'date_joined')
    get_detail = UserResourceDetail().get_detail

# ... register & use UserResource

+1

@dnozay ν•΄κ²° 방법에 λŒ€ν•΄ +1

+1 @dnozay , ꡉμž₯ν•΄

get_resource_uri κ°€ μ‚¬μš©μž μ„ΈλΆ€ 정보 λ³΄κΈ°μ—μ„œ μ˜¬λ°”λ₯΄κ²Œ μž‘λ™ν•˜λ„λ‘ ν•˜λ €λ©΄ UserResource κ°€ μ •μ˜λœ 후에 λ‹€μŒμ„ μΆ”κ°€ν•΄μ•Ό ν•©λ‹ˆλ‹€.

UserResourceDetail.get_resource_uri = UserResource().get_resource_uri

그렇지 μ•ŠμœΌλ©΄ resource_uri λŠ” λͺ¨λ“  μ„ΈλΆ€ μ‘λ‹΅μ—μ„œ λΉ„μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

κ΄€λ ¨ κ°€λŠ₯μ„±: #1265

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰