Django

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

Perodic Tasks By Celery 3.1 Example

I am assuming you have read celery docs from Celery Documentation As we know, celery can be used as a scheduler for executing asynchronous tasks in periodic cycles. Here I am going to share to do that with a code example. But I am going to avoid theoretical knowledge here because you can read them in celery documentation. First install celery: pip install django-celery. Configuring Brokers We are using django database as Broker. For detail understanding, check here: http://celery.readthedocs.org/en/latest/getting-started/brokers/django.html Making Periodic…

Keep reading

Working with Formset

As documentation says: A formset is a layer of abstraction to work with multiple forms on the same page. It can be best compared to a data grid. So here I am going to show a very simple django formset implementation example. Here we are going to use the following model, form, template, view: Model class Product(models.Model): name = models.CharField(max_length=50) quantity = models.IntegerField() price = models.IntegerField() class Distributor(models.Model): name = models.CharField(max_length=100) products= models.ManyToManyField(Product) These fairly simple models, where…

Keep reading

Syncdb vs Migration

While surfing through stackoverflow, I find a common question among Django users that, database not working properly; fields attribute changed, yet not working etc. Clearly because most of them used syncdb after altering fields. Well, lets make some things clear here about django syncdb and migration. What is syncdb? syncdb is a command which is executed in django shell to create tables for first time for apps which are added to INSTALLED_APPS of settings.py. Need to keep in mind about two key words: 'First Time' and 'Newly Added…

Keep reading