I have a simple working class based view inheriting UpdateView.
class UpdateModel(UpdateView):
model = ModelName
fields = ['field_1' , 'field_2' , ]
template_name_suffix = '_update_form'
success_url = reverse_lazy('home')
and the url that maps back to this view
url(r'^edit/(?P<pk>[\w-]+)$' , UpdateModel.as_view() , name="update_model"),
Now the issue I'm facing is that I usually encrypt the PK value before sending it outside(since they are sensitive). When I receive them back, I decrypt them to get the Model Object.
Brief working of encryption process.
def pk_encoder(pk):
int_pk = int(pk)
hashids = Hashids("MySalt")
encoded_pk = hashids.encode(pk_id)
return encoded_pk
ans similarly I use hashids.decode('string') to obtain my Model Object.
But in UpdateView there is no provision for this(that I know of). In urls.py it simply accepts the pk id to return the relevant form for updating a model. I understand that I have to override UpdateView or any of it's function in some way but can't figure out how. May I know how to edit this functionality of UpdateView
Thanks in advance.
It was a simple tweak. After reading the source code, here , I found that I simply had to extend the functionality of
def get_object(self)
which is declared in SingleObjectMixin(ContextMixin) class.
The code looks something like,
class UpdateModels(UpdateView):
#declare model, fields, template_name etc.
def get_object(self):
#decode and get the object in the variable desired_model_object
return desired_model_object
Hope this helps someone.