Skip to content

Instantly share code, notes, and snippets.

View samueleresca's full-sized avatar

Samuele Resca samueleresca

View GitHub Profile
{
"name":"demo",
"version":"0.0.0",
"main": "index.js",
"dependencies":{
"dep1":"1.2.x",
"dep2":"4.2.x"
}
}
{
"name":"demo",
"version":"0.0.0",
"main": "index.js",
"dependencies":{
"dep1":"1.2.x",
"dep2":"4.2.x"
},
"devDependencies":{
"mocha":"1.2.x"
{
"name":"demo",
"version":"0.0.0",
"main": "index.js",
"dependencies":{
"dep1":"1.2.x",
"dep2":"4.2.x"
},
"devDependencies":{
"mocha":"1.2.x"
"""
Django settings for project_name project.
"""
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', #DATABASE ENGINE PROVIDER
'NAME': path.join(PROJECT_ROOT, 'db.sqlite3'),
'USER': '',
'PASSWORD': '',
from django.db import models
# Each model extends models.Model
class Publisher(models.Model):
# Simple definition of string field
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
import django
django.setup()
#Imports all application models
from app.models import *
#Initialize new publishers
pub1= Publisher(name="O'Reilly Media", address="St. XXX",city= "Sebastopol", state_province="California", country="US", website="http://www.oreilly.com/")
pub1.save()
pub2= Publisher(name="Apress Media LLC", address="St. XXX",city= "New York City", state_province="New York", country="US", website="https://www.apress.com/")
pub2.save()
#Use all() method to retrieve all data
for pub in Publisher.objects.all():
print(pub.name)
#You can access to related tables
for book in Book.objects.all():
print(book.title + " - " + book.publisher.name)
#You can use filter method to filter data(Keyword WHERE in SQL)
filteredBooks = Book.objects.filter(publication_date__year="2002")
for b in filteredBooks:
#You can use get() method to retrieve a sigle entry
book = Book.objects.get(id=1).title
#To update a record simply set an attribute and save the enity
book.title = "Title 2"
book.save()
#You can use delete() method to remove data
book=Book.objects.get(id=1)
book.delete()