从同一项目中的另一个应用程序调用应用程序的基于类的视图

时间:2017-10-06 12:55:05

标签: django django-rest-framework

我有这个基于类的视图:

class Browse(APIView):
'''
Browse associations

'''
permission_classes = [TokenHasReadWriteScope]

def get(self,request,format=None):
    reply={}
    status=200


    try:
        filter_options={}
        name=request.query_params.get('name','').strip()
        if name:
            filter_options['name__icontains']=name


        associations=Association.objects.filter(**filter_options)values('id','name')
        page=request.query_params.get('page',1)
        paginator=Paginator(associations,20)
        data=paginator.page(page)
    except PageNotAnInteger:
        data=paginator.page(1)
    except EmptyPage:
        data=paginator.page(paginator.num_pages)

    except:
        status=400
        reply['detail']=(_('ErrorProcessingRequest'))

    if status==200:
        reply['associations']=list(data)
        reply['total_num_of_pages']=paginator.num_pages
        reply['total_rows_found']=paginator.count

    return JsonResponse(reply,status=status)

现在我有另一个面向内部用户的应用程序(不同的登录和所有)但我想列出关联仍然。上面的代码很简短,我不介意在那里粘贴它,但只是想知道我是否可以通过调用另一个应用程序的views.py浏览来避免DRY。

目前,我从app 2开始:

      from app1.views import Browse
      b=Browse()
      #but I cant serialize it as it returns <app1.views.Browse object at 0x0000000006CE0E80> is not JSON serializable

2 个答案:

答案 0 :(得分:1)

按照SO Answer解决问题。我所要做的就是:

structure(list(V1 = c("Eschweilera coriacea", "Eschweilera costaricensis",
"Eschweilera cyathiformis", "Eschweilera decolorans", "Eschweilera eperuetorum",
"Eschweilera fanshawei", "Eschweilera gigantea", "Eschweilera grandiflora",
"Eschweilera hondurensis", "Eschweilera integricalyx", "Eschweilera integrifolia",
"Eschweilera itayensis", "Eschweilera jacquelyniae", "Eschweilera juruensis",
"Eschweilera klugii", "Eschweilera laevicarpa", "Eschweilera longipedicellata",
"Eschweilera longirachis", "Eschweilera macrocarpa", "Eschweilera mattos-silvae",
"Eschweilera mexicana", "Eschweilera micrantha", "Eschweilera microcalyx",
"Eschweilera nana", "Eschweilera neblinensis", "Eschweilera neei",
"Eschweilera obversa", "Eschweilera ovalifolia", "Eschweilera ovata",
"Eschweilera pachyderma", "Eschweilera panamensis", "Eschweilera paniculata",
"Eschweilera parviflora", "Eschweilera parvifolia", "Eschweilera pedicellata",
"Eschweilera perumbonata", "Eschweilera piresii", "Eschweilera pittieri",
"Eschweilera potaroensis", "Eschweilera praealta", "Eschweilera pseudodecolorans",
"Eschweilera punctata", "Eschweilera rabeliana", "Eschweilera rankiniae",
"Eschweilera revoluta", "Eschweilera rhododendrifolia", "Eschweilera rhodogonoclada",
"Eschweilera rimbachii", "Eschweilera rionegrense", "Eschweilera rodriguesiana",
"Eschweilera roraimensis", "Eschweilera rufifolia", "Eschweilera sagotiana",
"Eschweilera sclerophylla", "Eschweilera sessilis", "Eschweilera simiorum",
"Eschweilera squamata", "Eschweilera subcordata", "Eschweilera subglandulosa",
"Eschweilera tenax", "Eschweilera tenuifolia", "Eschweilera tessmannii",
"Eschweilera tetrapetala", "Eschweilera truncata", "Eschweilera venezuelica",
"Eschweilera wachenheimii", "Foetidia africana", "Foetidia asymetrica",
"Foetidia capuronii", "Foetidia cuneata", "Foetidia delphinensis",
"Foetidia dracaenoides", "Foetidia macrocarpa", "Foetidia parviflora",
"Foetidia pterocarpa", "Foetidia rodriguesiana", "Foetidia rubescens",
"Foetidia sambiranensis", "Foetidia vohemarensis", "Grias cauliflora",
"Grias colombiana", "Grias haughtii", "Grias longirachis", "Grias multinervia",
"Grias neuberthii", "Grias peruviana", "Gustavia acuminata",
"Gustavia acuta", "Gustavia angustifolia", "Gustavia augusta",
"Gustavia brachycarpa", "Gustavia coriacea", "Gustavia dodsonii",
"Gustavia dubia", "Gustavia elliptica", "Gustavia erythrocarpa",
"Gustavia excelsa", "Gustavia flagellata", "Gustavia foliosa",
"Gustavia fosteri")), .Names = "V1", row.names = c(NA, 100L), class = "data.frame")

答案 1 :(得分:0)

使用此导入语句

from appName.views import viewName as variable_Name

稍后致电

return variable_Name(request)