日期:2014-05-16 浏览次数:20623 次
class Publisher(models.Model): 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) website = models.URLField() class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField(blank=True,verbose_name='e-mail') class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField(blank=True, null=True)
p1 = Publisher(name='Addison-Wesley', address='75 Arlington Street',city='Boston', state_province='MA', country='U.S.A.',website='http://www.apress.com/') p1.save()
p1 = Publisher.objects.create(name='Apress',address='2855 Telegraph Avenue',city='Berkeley', state_province='CA', country='U.S.A.',website='http://www.apress.com/')
p1.name = 'Apress Publishing' p1.save()
Publisher.objects.filter(id=1).update(name='Apress Publishing')
UPDATE book_publisher SET name = 'Apress Publishing', address = '2855 Telegraph Ave.', city = 'Berkeley', state_province = 'CA', country = 'U.S.A.', website = 'http://www.apress.com' WHERE id = 52;
UPDATE books_publisher SET name = 'Apress Publishing' WHERE id = 52;[/color]
Publisher.objects.all().update(country='USA')
Publisher.objects.all() 选择所有 Publisher.objects.filter(name= ‘ Apress ’ ) 数据过滤 Publisher.objects.filter(name__contains="press") Publisher.objects.get(name= “ Apress ” ) 获取单个对象
p = Publisher.objects.get(name="dddddddddddd") p.delete()
Publisher.objects.filter(country='USA').delete() Publisher.objects.all().delete()
b = Book.objects.get(id= 1 ) b.publisher b.publisher.website
p = Publisher.objects.get(name='Apress Publishing') p.book_set.all() p.book_set.filter(name__icontains='django')
b = Book.objects.get(id= 2 ) b.authors.all() b.authors.filter(first_name='Adrian') b.authors.filter(first_name='Adam')