一、增加

from django.db import models
class Publisher(models.Model):
    name = models.CharField(max_length=30, verbose_name="名称")
    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 Meta:
        verbose_name = '出版商'
        verbose_name_plural = verbose_name
 
    def __str__(self):
        return self.name
 
class Author(models.Model):
    name = models.CharField(max_length=30)
    def __str__(self):
        return self.name
 
class AuthorDetail(models.Model):
    sex = models.BooleanField(max_length=1, choices=((0, ''),(1, ''),))
    email = models.EmailField()
    address = models.CharField(max_length=50)
    birthday = models.DateField()
    author = models.OneToOneField(Author)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()
    price=models.DecimalField(max_digits=5,decimal_places=2,default=10)
    def __str__(self):
        return self.title
from app01.models import *

    #create方式一:   Author.objects.create(name='Alvin')
    #create方式二:   Author.objects.create(**{"name":"alex"})
#save方式一: author=Author(name="alvin") # 对象创建方式 author.save() #save方式二: author=Author() # 对象属性添加方式 author.name="alvin" author.save()

  增加一对多与多对多字段信息:

#一对多(ForeignKey):

    #方式一: 由于绑定一对多的字段,比如publish,存到数据库中的字段名叫publish_id,所以我们可以直接给这个
    # 字段设定对应值:
    Book.objects.create(title='php',
                        publisher_id=2,   #这里的2是指为该book对象绑定了Publisher表中id=2的行对象
                        publication_date='2017-7-7',
                        price=99)
    #方式二:
    #  <1> 先获取要绑定的Publisher对象:
        pub_obj = Publisher(name='河大出版社',address='保定',city='保定',
                            state_province='河北',country='China',website='http://www.hbu.com')
        # 或者
        pub_obj=Publisher.objects.get(id=1)

    #  <2>将publisher_id=2改为publisher=pub_obj
        Book.objects.create(title='php',
                            publisher=pub_obj,
                            publication_date='2017-7-7',
                            price=99)


#多对多(ManyToManyField()):
    author1=Author.objects.get(id=1)
    author2=Author.objects.filter(name='alvin')[0]
    book=Book.objects.get(id=1)
    book.authors.add(author1,author2)
    #等同于:
    book.authors.add(*[author1,author2])
    book.authors.remove(*[author1,author2])
    #-------------------
    book=models.Book.objects.filter(id__gt=1)
    authors=models.Author.objects.filter(id=1)[0]
    authors.book_set.add(*book)
    authors.book_set.remove(*book)
    #-------------------
    book.authors.add(1)
    book.authors.remove(1)
    authors.book_set.add(1)
    authors.book_set.remove(1)

#注意: 如果第三张表是通过models.ManyToManyField()自动创建的,那么绑定关系只有上面一种方式
    # 如果第三张表是自己创建的:
     class Book2Author(models.Model):
            author=models.ForeignKey("Author")
            Book=  models.ForeignKey("Book")
    #  那么就还有一种方式:
            author_obj=models.Author.objects.filter(id=2)[0]
            book_obj  =models.Book.objects.filter(id=3)[0]
            s=models.Book2Author.objects.create(author_id=1,Book_id=2)
            s.save()
            s=models.Book2Author(author=author_obj,Book_id=1)
            s.save()

二、删除

Book.objects.filter(id=1).delete()

  注意:表面上是删除了一条,实际上,Django会默认把這个记录关联的字段,全部删除!这种删除方式也称为级联删除。

三、修改

# 方式一
author = Author.objects.get(id=5)
author.name = 'teQ'
author.save()

# 方式二
Publish.objects.filter(id=2).update(name='teQ')

  注意:

    1.update不能修改一个单一对象,也就是说对于get()获取的数据,是不能用update.而filter()获取的数据是一个QuerySet对象,而update默认是修改一个数据集合的。

    2.注意以下区别:

#---------------- update方法直接设定对应属性----------------
    models.Book.objects.filter(id=3).update(title="PHP")
    ##sql:
    ##UPDATE "app01_book" SET "title" = 'PHP' WHERE "app01_book"."id" = 3; args=('PHP', 3)


#--------------- save方法会将所有属性重新设定一遍,效率低-----------
    obj=models.Book.objects.filter(id=3)[0]
    obj.title="Python"
    obj.save()
# SELECT "app01_book"."id", "app01_book"."title", "app01_book"."price", 
# "app01_book"."color", "app01_book"."page_num", 
# "app01_book"."publisher_id" FROM "app01_book" WHERE "app01_book"."id" = 3 LIMIT 1; 
# 
# UPDATE "app01_book" SET "title" = 'Python', "price" = 3333, "color" = 'red', "page_num" = 556,
# "publisher_id" = 1 WHERE "app01_book"."id" = 3; 
View Code

相关文章:

  • 2021-05-18
  • 2022-12-23
  • 2022-02-21
  • 2022-01-25
  • 2022-12-23
  • 2021-06-21
猜你喜欢
  • 2021-10-13
  • 2022-12-23
  • 2021-07-19
  • 2021-05-26
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案