jrief / django-angular

Let AngularJS play well with Django
http://django-angular.awesto.com/
MIT License
1.23k stars 293 forks source link

Proper way to get NgCRUDView to return get_<field>_display #196

Closed dwatkinsweb closed 9 years ago

dwatkinsweb commented 9 years ago

I have a model set up to use an NgCRUDView. It's working correctly as far as I can tell, except I don't want it to return 'J' or 'N', but rather 'Major' or 'Minor' respectively.

I was able to get it working using the following overrides:

    def get_display_object(self, item):
        # return dictionary with expected formats/display values

    def ng_get(self, request, *args, **kwargs):
        return JSONResponse(self.get_display_object(self.get_object()))

    def ng_query(self, request, *args, **kwargs):
        return JSONResponse([self.get_display_object(i) for i in self.get_queryset()])

I wasn't sure if there was a better way to handle this or if I should even really be using NgCRUDView for something like this.

jkosir commented 9 years ago

Django core serializers don't support anything but model fields, e.g. can't add get_FOO_display or any attribute to fields kwarg for serializer.serialize() call.

So yeah, you'll have to add the display info manually. Maybe you could override the build_json_response method to use restframework serializers, or drop crud view entirely in favour of rest framework while you're at it.

dwatkinsweb commented 9 years ago

Thank you. I think I'll move to rest framework. I was thinking of integrating that anyways for other tools to use the information and that will probably make it a bit easier.

Thanks