
Untitled
By: a guest on
Jun 11th, 2012 | syntax:
None | size: 0.98 KB | hits: 9 | expires: Never
Django model with crud operations
from django.db import models
from datetime import datetime
class Name(models.Model):
zipcode = models.CharField(max_length=5, primary=True, blank=False)
city = models.CharField(max_length=50, blank=False)
state = models.CharField(max_length=2, blank=False)
latitue = models.CharField(max_length=15, blank=False)
longitue = models.CharField(max_length=15, blank=False)
curr_time = models.datetime(default=datetime.now, blank=False)
from models import Name
from datetime import datetime
# create a new model
name = Name(city='New York', state='NY')
# fields can also be set this way
name.zipcode = '10027'
# save the model to the database
name.save()
# find a model by zipcode
name = Name.objects.filter(zipcode='10027')
# modify it
name.curr_time = datetime.now()
# save it
name.save()
curr_time = models.DateField(auto_now=True)
# or auto_now_add=True, if you want set this field only at the creation.