ajax - Is it ok for a Django mixin to inherit another mixin? -


i'm pretty sure answer question "no", since django mixins supposed

inherit "object"s, can't find alternative solution problem :(

to make question simple possible,,,

views.py

class jsonresponsemixin(object):     def render_to_response(self, context):         "returns json response containing 'context' payload"         return self.get_json_response(self.convert_context_to_json(context))      def get_json_response(self, content, **httpresponse_kwargs):         "construct `httpresponse` object."         return http.httpresponse(content,                                  content_type='application/json',                                  **httpresponse_kwargs)      def convert_context_to_json(self, context):         "convert context dictionary json object"         # note: *extremely* naive; in reality, you'll need         # more complex handling ensure arbitrary         # objects -- such django model instances or querysets         # -- can serialized json.         return json.dumps(context)   class handlingajaxpostmixin(jsonresponsemixin):     def post(self, request, *args, **kwargs):         .....         data = {'somedata': somedata}          return jsonresponsemixin.render_json_response(data)   class userdetailview(handlingajaxpostmixin, detailview):     model = myuser     ..... 



problem have that, multiple views, want respond "post" request same

json response. why defined handlingajaxpostmixin reuse

other views. since handlingajaxpostmixin returns json response,

it requires render_json_response method, defined in jsonresponsemixin.

this reason why making handlingajaxpostmixin inherit

jsonresponsemixin, seems wrong :(..

any suggestions..?

thanks!!!

it's valid mixin inherit mixin - in fact, how of django's more advanced mixins made.

however, idea of mixins reusable parts that, other classes, build complete, usable class. right now, jsonresponsemixin might separate class don't inherit from, or methods might module-wide methods. works, there's nothing wrong it, that's not idea of mixin.

if @ django's basedetailview, see following get() method:

def get(self, request, *args, **kwargs):     self.object = self.get_object()     context = self.get_context_data(object=self.object)     return self.render_to_response(context) 

get_object() , get_context_data() defined in subclasses of basedetailview, render_to_response() isn't. it's okay mixins rely on methods it's superclasses don't define, allows different classes inherit basedetailview supply own implementation of render_to_response(). right now, in django, there's 1 subclass, though.

however, logic delegated as possible small, reusable methods mixins supply. that's want aim for. if/else logic avoided as possible - advanced logic in django's default views is:

if form.is_valid():      return self.form_valid(form)  else:      return self.form_invalid(form) 

that's why similar views, createview , updateview in fact 2 separate views, while single view additional if/else logic. difference createview self.object = none, while updateview self.object = self.get_object().

right using detailview defines get() method returns result of self.render_to_response(). however, override render_to_response() return json response instead of template-based html response. you're using mixin don't use (singleobjecttemplateresponsemixin) , override it's behavior don't want either, view doing want do. better idea write alternative detailview who's job supply json response based on single object. this, create singleobjectjsonresponsemixin, similar singleobjecttemplateresponsemixin, , create class jsondetailview combines needed mixins single object:

class singleobjectjsonresponsemixin(object):     def to_json(context):         return json.dumps(context)      def render_to_response(context, **httpresponse_kwargs):         return httpresponse(self.to_json(context),                             context_type='application/json',                             **httpresponse_kwargs)  class basejsondetailview(singleobjectmixin, view):     # if want same get, inherit basedetailview     def post(self, request, *args, **kwargs):         self.object = self.get_object()         context = self.get_context_data(object=self.object)         return render_to_response(context)  class jsondetailview(singleobjectjsonresponsemixin, basejsondetailview):     """      return json detail data of single object.     """ 

notice same basedetailview , singleobjecttemplateresponsemixin provided django. difference define post() method , rendering more simple conversion json of context data, not complete template rendering. however, logic deliberately kept simple as possible, , methods don't depend on each other separated as possible. way, singleobjectjsonresponsemixin can e.g. mixed baseupdateview create ajax/json-based updateview. subclasses can override different parts of mixins, overriding to_json() supply data structure. rendering logic belongs (in render_to_response()).

now need create specific jsondetailview subclass , define model use:

class userjsondetailview(jsondetailview):     model = myuser 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -