Django learning knowledge point summary (model)

Question 1:

Database query, it appears [<Publisher: Publisher object>, <Publisher: Publisher object>]

Steps:

1.python manage.py shell into django's shell programming

2. from books.models import Publisher

>>> publisher_list = Publisher.objects.all()
>>> publisher_list
[<Publisher: Publisher object>, <Publisher: Publisher object>]

We can easily solve this problem by adding a method  __str__()  to the  Publisher  object.  The __str__()  method tells Python how to use the object as a string. please look below:

Change the model.py file

 

from django.db import models

class Publisher(models.Model):
    name = models.CharField(maxlength=30)
    address = models.CharField(maxlength=50)
    city = models.CharField(maxlength=60)
    state_province = models.CharField(maxlength=30)
    country = models.CharField(maxlength=50)
    website = models.URLField()

    **def __str__(self):**
        **return self.name**

class Author(models.Model):
    salutation = models.CharField(maxlength=10)
    first_name = models.CharField(maxlength=30)
    last_name = models.CharField(maxlength=40)
    email = models.EmailField()
    headshot = models.ImageField(upload_to='/tmp')

    **def __str__(self):**
        **return '%s %s' % (self.first_name, self.last_name)**

class Book(models.Model):
    title = models.CharField(maxlength=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    **def __str__(self):**
        **return self.title**

As you can see, the __str__() method returns a string. __str__() must return a string. If it is any other type, Python will throw a TypeError error message "__str__ returned non-string" .          

In order for our changes to take effect, exit the Python Shell, and then run python manage.py shell again to enter. Listing Publisher objects is now easy to understand:      

>>> from books.models import Publisher
>>> publisher_list = Publisher.objects.all()
>>> publisher_list
[<Publisher: Addison-Wesley>, <Publisher: O'Reilly>]

Make sure you include the __str__() method in each of your models, not only for interaction, but also because Django uses __str__() in other places to display objects.   

Finally, __str()__ is also a good example of how we can add behavior to a model. Django's models not only define the structure of database tables for objects, but also their behavior. __str__() is an example to demonstrate that models know how to display themselves.      

 

Question two

Use order_by() to sort by a certain field, you can write it in the template

Common method:

>>> Publisher.objects.order_by("-name")
[<Publisher: O'Reilly>, <Publisher: Apress Publishing>, <Publisher: Addison-Wesley>]

The default sort field is defined in model.py

 

class Publisher(models.Model):
    name = models.CharField(maxlength=30)
    address = models.CharField(maxlength=50)
    city = models.CharField(maxlength=60)
    state_province = models.CharField(maxlength=30)
    country = models.CharField(maxlength=50)
    website = models.URLField()

    def __str__(self):
        return self.name

    **class Meta:**
        **ordering = ["name"]**

you can directly write

In [3]: Publisher.objects.order_by()
Out[3]: [<Publisher: Addison-Wesley>, <Publisher: O'Reilly>, <Publisher: Apress>]

Question three

delete field

Removing a field from a model is much easier than adding it. To delete a field just do the following:

Remove this field from your model and restart the web server.

Drop the column from your database using the command shown below:

ALTER TABLE books_book DROP COLUMN num_pages;

Remove Many-to-Many fields

Because the many-to-many field is somewhat different from the normal field, its deletion process is also different:

Delete the ManyToManyField in your model and restart the web server.  

Drop the many-to-many table in your database using the command shown below:

DROP TABLE books_books_publishers;

delete model

Removing a model entirely is as easy as removing a field. Deleting a model only requires the following steps:

Remove this model from your models.py file and restart the web server.  

Delete this table from your database using the following command:

DROP TABLE books_book;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326841516&siteId=291194637