Interview Questions

Python - Django Model Creation

The following code illustrates a sample ORM object model in Django.
This class can be used for persisting objects in DB.

from django.db import models
import datetime,uuid

class Device(models.Model):
    '''
    classdocs
    '
''
   
    id = models.CharField(max_length=70,primary_key=True)
     
    name = models.CharField(max_length=70)
   
    status = models.CharField(max_length=70)
   
    ipAddress = models.CharField(default=False)
   
    createdOn = models.DateTimeField(datetime.datetime.now,blank=True)
   
   
    def __unicode__(self):
        return self.id
   
    def save(self, *args, **kwargs):
        print "saving model Device"
        self.id = uuid.uuid1()
        self.createdOn = datetime.datetime.now()
        super(Device, self).save(*args, **kwargs)
   
    class Meta:
        app_label = 'Tdd' # app name in django project
        db_table = 'Device' # table name for the model