Dynamic Models in Django-Part1 (adding new fields to models from admin)

Sometimes at the production level, there may be a chance of adding new fields to our model.With the normal Django models when we add a 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 anywhere in your models.py

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

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

from django.contrib import admin
from eav.forms import BaseDynamicEntityForm
from eav.admin import BaseEntityAdminclass 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 programmatically and detailed explanation of Attributes and Values.

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to index binary files in django haystack Ramya Ambati

Now we are going to index text content which is stored in structured files such as PDFs, Microsoft Office documents, images, etc using haystack and ...

Continue Reading...
Understanding Django's model formsets in detail and their advanced usage. Ramya Ambati

Silmilar to the regular formsets, django also provide model formset that make it easy to work with django models. Django model formsets provide a way ...

Continue Reading...
Django Model managers and properties Sandeep Jagata

Django model is the single, definitive source of data about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties