Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
the django model cheat sheet

the django model cheat sheet



|Views: 11,559 |Likes:
Published by windoze007
this is a two page, short, concise, example based cheat sheet for django's model.Model object
this is a two page, short, concise, example based cheat sheet for django's model.Model object

More info:

Published by: windoze007 on Aug 26, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





django_db_cheat.txtPage 1 of 2 Aug/2008
1#Model of question:2
 Blog(models.Model): 3 name=models.CharField(max_length=100) 4 tagline=models.TextField() 5 bestEntry=ForeignKey("mutually_referential.Entry")  6# two way relations usually illegal unless specified as referential7# myId = models.AutoField(primary_key=True)8# by default the primary key is called 'id' - but overridable9# 'blog.pk' to will refer to the primary key regardless of what its called1011
 Author(models.Model): 12 name=models.CharField(max_length=50) 13 email=models.EmailField() 1415
 Entry(models.Model): 16 blog=models.ForeignKey(Blog)  17# automagically in any Blog instance b, there is now a set called b.entry_set18# blog = models.ForeignKey(Blog, related_name='custom_name')19# (this overrides default name of blog.entry_set with blog.custom_name)20 headline=models.CharField(max_length=255) 21 body_text=models.TextField() 22 pub_date=models.DateTimeField() 23 authors=models.ManyToManyField(Author) 24
 Meta:25 ordering= ('-pub_date', 'headline') 26
 articles_from_same_day(self): 27
Entry.objects.filter(pub_date=self.pub_date).exclude(id=self.id) 28# a.articles_from_same_day() returns a resultset2930
 EntryDetail(models.Model): 31 entry=models.OneToOneField(Entry) 32 details=models.TextField() 3334#Inserting35 b=Blog(name='Beatles Blog',tagline='All the latest Beatles news.') 36 b.save() 37 bb=Blog(id=3,name='Not Cheddar',tagline='Anything but cheese.') 38 bb.save() # Overrides the previous blog with ID=3! 3940#Manager41 Entry.objects.get(id=2) # gets id=2 42 e=Entry.objects.select_related().get(id=2) 43
e.blog# Doesn't hit the database twice; uses cached version. 4445#Filter and Exclusion - it is a lazy operation, not done until iterated/read46 Entry.objects.filter(pub_date__year=2006) 47 Entry.objects.filter( 48 headline__startswith='What').exclude( 49 pub_date__gte=datetime.now()).filter( 50 pub_date__gte=datetime(2005, 1, 1))  51 Entry.objects.filter(blog=b) # Query using object instance 52 Entry.objects.filter(blog=b.id) # Query using id from instance 53 Entry.objects.filter(blog=5) # Query using id directly 5455#Sorting56 Entry.objects.order_by('pub_date', '-headline') 5758# Deleting59 e.delete() #If there are dependencies, they will be deleted too 60 Entry.objects.filter(pub_date__year=2005).delete() 61# warning - when deleting a resultset, you might delete many entries6263# Updating64 e.headline= 'Everything is the same' 65 e.save()
- 1 -

Activity (44)

You've already reviewed this. Edit your review.
Shiva Krishna added this note
Good one
Shiva Krishna liked this
1 thousand reads
1 hundred reads
Adam Buran liked this
Hanse69 liked this
Hanse69 liked this
Gregory Nicholas liked this
Gregory Nicholas liked this
Gregory Nicholas liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->