doraemon of python (major update) configuration, and application examples to explain the library management system

# ## 11.3 Library Management System 

# ### 11.3.1 Publishing Management 

show:

 - Design the URL of

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

 - write function

 - `` `Python
   from django.shortcuts Import the render
   from app01 Import Models 
  
  # show Press 
  DEF publisher_list (Request):
       # query from the database publishing information 
      all_publishers = models.Publisher.object.all ()
       #Press return to a page containing information 
      return the render (Request, ' publisher_list.html ' , { ' All-Publishers ' : all_publishers}) 
  `` `

 - Write Template

 - ` `` HTML
   <Table Boder = ' . 1 ' > 
      < thead> 
          <TH> ID </ TH> 
          <TH> ID </ TH> 
          <TH> name </ TH> 
      </ thead> 
      
      { % for Publisher in all_publishers% }
           <TR> 
              <TD> {} {forloop.counter } </ td>   # Own function, to generate a corresponding number
              <TD> publisher.pk {{}} </ TD>     # PK primary key is the primary key 
              <TD> publisher.name {{}} </ TD> 
          </ TR> 
  </ Table> 
  `` ` 

add: 

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

delete 

`` `Python 
obj_list = models.Publisher.objects.filter (PK = PK) 
obj_list.delete () 

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

edit 

` `` Python 
# modify data 
# first of all to get the data 
obj = models.Publisher.objects.filter (name)    #Irrespective repeated, like the case of null 
obj.name = publisher_name 
obj.save 
`` ` 



# ### 11.3.2 book management 



` `` Python 
class Book (models.Model): 
    title = models.CharField (MAX_LENGTH = 32 ) 
    Pub = models.ForeignKey ( ' Publisher ' , on_delete = models.CASCADE) # cascade delete, delete a table, also remove other corresponding 
    
on_delete parameters: models.CASCADE models.SET_DEFAULT models.SET_null 
`` ` 

query: 

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

for Book in all_book:
     Print (book.title)
    Print (book.pub)    # Press objects associated with 
    Print (book.pub.pk)    # to check more than one query id 
    Print (book.pub_id)    # stand isolated in the book table ID 
    Print (book.pub. name) 
`` ` 



add: ` 

`` Python 
models.Book.object.create (title = book_name, Pub = Press objects) 
models.Book.object.create (title = book_name, pub_id = pub_id) 
`` ` 

delete: 

`` `Python 
PS = request.GET ( ' ID ' ) 
models.Book.object.filter (PK = PK) .Delete () 
` `` 



editor: 

`` `HTML 
{% IF book_obj.pub == Publisher% }
     <Option Selected value = " {} {} publisher.pk " > publisher.name {{}} </ Option> 
{ % the else % }
     <Option value = " {{Publisher. }} PK " > publisher.name {{}} </ Option> 
{ % endif% } 
` `` 

`` `Python 
# modify data 
book_obj.title = BOOK_NAME 
book.obj.pub = models.Publisher.objects.get ( = PK pub_id) 
book_obj.save () 
`` ` 

# ### 11.3.3 of management
 
1 . create a list of 

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

 2 . show 

` `` Python 
# display of 
DEF author_list (Request):
     # queried All objects 
    all_author = models.Author.object.all ()
     return the render (Request, ' author_list.html ' , { ' all_author ' : all_author}) 
    
`` ` 

` `` HTML 
# write template 
{%   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 . increase 

` `` Python 
author_obj = models.Author.objects.create (name = author_name) # only be inserted into the table of contents book 
author_obj = models.set (book)    # Set the authors and books relations-many 
`` `

 4 . modify 

` `` Python 
Book = request.POST.getlist ( ' Book ' )     # get more than worth the time to use getlist 

# modify an object's data 
author_obj.name = name 
author_obj.save () 
# -many relationship 
author_obj.book.set (Books)    # each re-set 
`` ` 

# #### 11.3.3.1 Django set up three ways-many relationship

. 1 .django help us to generate the third table 

`` `Python 
class Author (models.Model): 
    name = models.CharField (MAX_LENGTH = 32 ) 
    Books = models.ManyToManyField ( ' Book ' )   # is not produced in the Author table fields, production third table 
`` `

 2 . to create their own third table 

` `` 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 . table self and in combination 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()
```

 

Guess you like

Origin www.cnblogs.com/doraemon548542/p/11595406.html