Change Column Header Dynamically in Django Tables2

Django Tables2 is a package which displays table directly from queryset. It shows column header based on object's attribute's name. But if someone wants to override it, how can he/she do that? Here is a easy solution. Suppose we have a model class like this: class SomeModel(models.Model): somevalue = models.CharField() And we want to show table column somevalue to overridenvalue Table Class: class SomeTable(tables.Table): def __init__(self, *args, _overriden_value="",**kwargs): super().__init__(*args, **kwargs) self.base_columns['somevalue'].verbose_name = _overriden_value class Meta:…

Keep reading

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

How to configure Django/Tornado in PyCharm Community Edition

I think, Pycharm is THE best IDE for developing python. But unfortunately, the professional edition is not free. But community edition is good enough for doing debugging, integrating GIT etc. Normally its easy to use the community edition for django and tornado's debugging/running if you know how to configure. Django For django's configuration, there is 5 easy steps: First: Go to edit configuration and click on it(like the below pictures). Second: Click on the (+) mark in top-left corner and add python configuration. Third: Click on the Script, and…

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