Some times in the production level there may be a chance of adding new fields to our model.With the normal django models when we add new field to the model the database is not able to identify the new field.To make database identify the new field we have to drop the existing database from the database and sync our application again.During the time of production it is not possible to remove the existing data. The solution for this is Django-eav.With this we can add any no of fields to our models on fly.

Steps To add New fields to the models Using django-eav: Step1:Install django-eav using the following command

pip install -e git+git://github.com/mvpdev/django-eav.git#egg=django-eav

Step2:Add 'eav' to your INSTALLED_APPS in your project’s settings.py file. Step3:Register your models with eav.

To register any model with EAV, you simply need to add the registration line somewhere that will be executed when django starts:

import eav
eav.register(Patient)

Keep the above lines any where in your models.py

Step4:Configure your models so that you can create new fields from admin it self

You can even have your eav attributes show up just like normal fields in your models admin pages. Just register using the eav admin class:

from django.contrib import admin
from eav.forms import BaseDynamicEntityForm
from eav.admin import BaseEntityAdmin

class PatientAdminForm(BaseDynamicEntityForm):
    model = Patient

class PatientAdmin(BaseEntityAdmin):
    form = PatientAdminForm

admin.site.register(Patient, PatientAdmin)

After following all the above steps sync your project with database using

python manage.py syncdb

Run your project using

python manage.py runserver

command and go to your admin page and add new Attributes(fields) to the Existing model. There you can add new attributes to your model. In Part-2 of This tutorial we'll see how to add attributes programatically and detailed explanation of Attributes and Values.

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties
Latest Comments
Related Articles
How to develop RESTful webservice in Django using djangorestframework? Chaitanya Kattineni

DjangoRestFramework is widely used to develop restful API. DjangoRestFramework is easy to use who are familiar with Django, as it is developed on the core ...

Continue Reading...
Vim for Python Web Development Ashwin Kumar

Having a good environment setup is important for effective, fast and easy coding. We have different IDE's like eclipse, pycharm, sublime etc.. which are powerful ...

Continue Reading...
How to create Custom User Model in Django? Chaitanya Kattineni

Django provides built in authentication which is good for most of the cases, but you may have needs that are being served with the existing ...

Continue Reading...