Created
May 15, 2011 04:54
-
-
Save saturngod/972897 to your computer and use it in GitHub Desktop.
database queries in django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Blog(models.Model): | |
name = models.CharField(max_length=100) | |
tagline = models.TextField() | |
def __unicode__(self): | |
return self.name | |
class Author(models.Model): | |
name = models.CharField(max_length=50) | |
email = models.EmailField() | |
def __unicode__(self): | |
return self.name | |
class Entry(models.Model): | |
blog = models.ForeignKey(Blog) | |
headline = models.CharField(max_length=255) | |
body_text = models.TextField() | |
pub_date = models.DateTimeField() | |
mod_date = models.DateTimeField() | |
authors = models.ManyToManyField(Author) | |
n_comments = models.IntegerField() | |
n_pingbacks = models.IntegerField() | |
rating = models.IntegerField() | |
def __unicode__(self): | |
return self.headline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#create Blog | |
from blog.models import Blog | |
b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.') | |
b.save() | |
#entry | |
from blog.models import Entry | |
entry = Entry.objects.get(pk=1) | |
cheese_blog = Blog.objects.get(name="Cheddar Talk") | |
entry.blog = cheese_blog | |
entry.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment