python - Standard way to pull a specific model from a common view in Django/DRF? -


i have 2 models same (inheriting base model), have problems referring them common view:

models:

class basemodel(models.model):     name = models.charfield(...)     owner = foreignkey(...)  class cat(basemodel):     ...  class dog(basemodel):     ... 

view:

class commonviewset(viewsets.modelviewset):     @link()     def set_owner(self, request, pk=none):          #how cat or dog models cleanly here?          #super fugly/unstable way         url_path = request.meta['path_info']         if 'cats' in url_path:             cat.objects.get(pk=pk).owner = ...         elif 'dogs' in url_path:             dog.objects.get(pk=pk).owner = ... 

i can put set_owner link in separate views, feels un-dry. in advance looking this!

you can pass model use in as_view method of class:

url(r'^cats/my-url/$', commonviewset.as_view(model=cat)), 

the modelviewset class inherits django's view class, set model attribute on instance of viewset. can use self.model right model current url.


Comments

Popular posts from this blog

Linux vanilla kernel on QEMU and networking with eth0 -

rdbms - what exactly the undo information lives in oracle? -

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -