Dynamically constructing filters based on string input using SQLAlchemy

Here I am going to write a dynamic filter. This filter is made for python 3. It will take query or model class and filter condtion as input, It will return filtered query based on those filter condition. This is constructed using this SO answer. Function: class DynamicFilter(ModelHelper): def __init__(self, query=None, model_class=None, filter_condition=None): super().__init__(*args, **kwargs) self.query = query self.model_class = model_class self.filter_condition = filter_condition def get_query(self): ''' Returns query with all the objects :return: '''…

Keep reading

Some Useful Tools/Function for Django

I am going to share some useful Django tools/functions which are very useful(were for me atleast) to get things done. Return any model class and its properties This method will return any model class if you have the name of the class. from django.db import models def get_model_description(model_name=None, return_property_list=True): for item in models.get_models(include_auto_created=True): if item.__name__ == model_name: if return_property_list is True: return item.get_trigger_properties() else: return item return…

Keep reading

Implementation of 'Forgot/Reset Password' feature in Django

Django has its own implementation for reset/forgot password for its admin site. We are going to use that piece of code as reference to implement similar feature for a non admin-site authentication page. Although there are tons of good packages which will allow user to use their password reseting system. But if the system isn't too complex and doesn't need such authentication plugins, then reusing the django's very own implementation can be a good option. Class based view is going to be used instead of method based view(for…

Keep reading