django学习之models2

models.py

class Entry(models.Model):
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name

class Blog(models.Model):
    name = models.CharField(max_length=30)
    entry = models.ForeignKey(Entry)

    def __unicode__(self):
        return self.name

创建对象:

    from blog.models import Entry,Blog

    entry1=Entry.objects.create(name='zhang')

    entry2=Entry.objects.create(name='li')

    blog1=Blog.objects.create(name='jlan_blog1',Entry=entry1)

    blog1=Blog.objects.create(name='jlan_blog2',Entry=entry2)

查看对象:

    entry1.name

        Out[25]: 'zhang'

    blog1.entry

        Out[1]:<Entry: zhang>

    blog1.name

        Out[14]:'jlan_blog1'
        
    entry1.blog_set.all()
        Out[33]: [<Blog: Blog object>]


你可能感兴趣的:(django)