码迷,mamicode.com
首页 > 编程语言 > 详细

doraemon的python(大更新) 实例讲解 图书管理系统的配置和应用

时间:2019-09-27 01:14:22      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:request   max   model   pos   any   sele   att   管理   cascade   

### 11.3 图书管理系统

#### 11.3.1 出版社的管理

展示:

- 设计URL

- ```python
  urlpatterns = [
      url(r^publisher_list,views.publisher_list)
  ]
  ```

- 写函数

- ```python
  from django.shortcuts import render
  from app01 import models
  
  #展示出版社
  def publisher_list(request):
      #从数据库中查询到出版社的信息
      all_publishers = models.Publisher.object.all()
      #返回到一个包含出版社信息的页面
      return render(request,publisher_list.html,{all-publishers:all_publishers})
  ```

- 写模板

- ```html
  <table boder = 1>
      <thead>
          <th>序号</th>
          <th>ID</th>
          <th>名称</th>
      </thead>
      
      {% for publisher in all_publishers %}
          <tr>
              <td>{{forloop.counter}}</td>   #自带的功能,生成对应序号
              <td>{{publisher.pk}}</td>    #pk就是primary key主键
              <td>{{publisher.name}}</td>
          </tr>
  </table>
  ```

新增:

```python
obj = models.Publisher.object.create(name=publisher_name)
```

删除

```python
obj_list = models.Publisher.objects.filter(pk=pk)
obj_list.delete()

obj = models.Publisher.objecct.get(pk=pk)
obj.delete()
```

编辑

```python
#修改数据
#首先要获取数据
obj = models.Publisher.objects.filter(name)   # 不考虑重复、空值等情况
obj.name = publisher_name
obj.save
```



#### 11.3.2 书籍管理



```python
class Book(models.Model):
    title = models.CharField(max_length=32)
    pub = models.ForeignKey(Publisher,on_delete=models.CASCADE) #级连删除,一个表删除,另一个对应的也会删除
    
on_delete的参数:models.CASCADE models.SET_DEFAULT models.SET_null
```

查询:

```python
all_book = models.Book.objects.all()

for book in all_book:
    print(book.title)
    print(book.pub)   #所关联的出版社对象
    print(book.pub.pk)   #查id  多了一次查询
    print(book.pub_id)   #支架在book表中查出的ID
    print(book.pub.name)
```



新增:

```python
models.Book.object.create(title=book_name,pub=出版社的对象)
models.Book.object.create(title=book_name,pub_id=pub_id)
```

删除:

```python
ps = request.get(id)
models.Book.object.filter(pk=pk).delete()
```



编辑:

```html
{% if book_obj.pub == publisher%}
    <option selected value="{{publisher.pk}}">{{publisher.name}}</option>
{% else %}
    <option value="{{publisher.pk}}">{{publisher.name}}</option>
{% endif %}
```

```python
#修改数据
book_obj.title = book_name
book.obj.pub = models.Publisher.objects.get(pk=pub_id)
book_obj.save()
```

#### 11.3.3 作者管理

1.创建作者表

```python
class Author(models.Model):
    name = models.CharField(max_length=32)
    book = models.ManyToManFieldy(Book)
```

2.展示

```python
#展示作者
def author_list(request):
    #查询到所有对象
    all_author = models.Author.object.all()
    return render(request,author_list.html,{all_author:all_author})
    
```

```html
#写模板
{%  for author in all_author%}
    <tr>
        <td>{{forloop.counter}}</td>
        <td>{{author.pk}}</td>
        <td>{{author.name}}</td>
        <td>
            {% for book in author.book.all%}
                {%  if forloop.last%}
                    《{{book.title}}》
                {% else %}
                    《{{book.title}}》
                {%endif%}
            {%endfor%}
        </td>
    </tr>
{%endfor%}
```

3.增加

```python
author_obj = models.Author.objects.create(name=author_name) #只插入到book表中的内容
author_obj = models.set(book)   #设置作者和书籍的多对多的关系
```

4.修改

```python
book = request.POST.getlist(book)    #获取多个值得时候要用getlist

#修改对象的数据
author_obj.name = name
author_obj.save()
#多对多的关系
author_obj.book.set(books)   #每次重新设置
```

##### 11.3.3.1 django设置多对多关系的三种方法

1.django帮助我们生成第三张表

```python
class Author(models.Model):
    name = models.CharField(max_length=32)
    books = models.ManyToManyField(Book)  # 不在Author表中生产字段,生产第三张表
```

2.自己创建第三场表

```python
class AuthorBook(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    date = models.DateField()
```

3.自建的表和ManyToMany联合使用

```python
class Author(models.Model):
    name = models.CharField(max_length=32)
    books = models.ManyToManyField(Book,through=AuthorBook)  # 不在Author表中生产字段,生产第三张表


class AuthorBook(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    date = models.DateField()
```

 

doraemon的python(大更新) 实例讲解 图书管理系统的配置和应用

标签:request   max   model   pos   any   sele   att   管理   cascade   

原文地址:https://www.cnblogs.com/doraemon548542/p/11595406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!