Model manipulation statements --note

from books.models import Publisher

 

insert:

p1 = Publisher(name = 'Apress', address = '2855 Telegraph Avenue', city = 'Berkeley', state_province = 'CA', country = 'U.S.A', website = 'http://www.apress.com/')

p1.save()

Step insert (create):

p1 = Publisher.objects.create(name = 'Apress', address = '2855 Telegraph Avenue', city = 'Berkeley', state_province = 'CA', country = 'U.S.A', website = 'http://www.apress.com/')

 

Find:

publish_list = Publish.objects.all()

publish_list

__unicode __ () method returns the value of the plurality of columns slightly more complicated, first_name and last_name field value and then to return the connection space, as follows:

def __unicode__(slef):

  return u'%s %s'%(self.first_name, self.last_name)

  // The only requirement for __unicode __ () is that it should return a unicode objects If `` __unicode __ () `` method does not return a Unicode object, for example, returns an integer number, then Python will throw a `` TypeError `` error, and prompts: "coercing to Unicode: need string or buffer, int found".

Publisher.objects.filter (name = 'Apress') // filter corresponds sql statement where

Publisher.objects.filter (country = 'USA', state_province = 'CA') // equivalent to sql statement where ... and ...

Publisher.objects.filter (name__contains = 'press') // contains a double underscore will be translated into sql statement in the like

  select id,name,address,city,state_province,conntry,website from books_publisher where name like '%press%';

Some other search types: icontains (case insensitive like); startswith and endswith; there range (SQL queries in between)

Gets a single object (ie, a result set QuerySet, if the query result is more than one object, will cause an abnormal; if the query does not return results will throw an exception)

Publisher.objects.get(name = 'Apress')

This  DoesNotExist  exception is an attribute of this Publisher model class, that  Publisher.DoesNotExist . In your application, you can catch and handle the exception, like this:

try:    
  p = Publisher.objects.get(name='Apress') except Publisher.DoesNotExist: print "Apress isn't in the database yet." else: print "Apress is in the database."

Publisher.objects.order_by("name")

Publisher.objects.order_by("state_province", "address")

Publisher.objects.order_by ( "- name") // reverse order

Django lets you specify the default sort model:

Publisher class (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 () 

    DEF __unicode __ (Self): 
        return the self.name 

    class Meta: 
        Ordering = [ 'name'] 
  // you can use any of a model class  Meta  class to set some options related to a particular model. There are in Appendix B  Meta  complete reference of all options, and now, we are concerned about  ordering  this option is enough.
Publisher.objects.filter (countru = 'USA') order_by. ( "- name") // chain query
Publisher.objects.order_by ( 'name') [0 ]
  //相当于sql: select id,name,address,city,state_province,country,website from book_publisher order by name limit 1;
Publisher.objects.order_by('name')[0,2]
  //相当于sql: select id,name,address,city,state_province,country,website from book_publisher order by name offset 0 limit 2;
Publiser.objects.order_by ( "- name") [ 0] // find the "last" 


update:

p1.name = 'Apress Publishing'

p1.save()

p1.id // assume result 52

以上两句相当于:update books_publisher set name='Apress Publishing',address = '2855 Telegraph Avenue', city = 'Berkeley', state_province = 'CA', country = 'U.S.A', website = 'http://www.apress.com/' where p1.id=52

After the improvements:

Publisher.objects.filter(id = 52).update(name = 'Apress Publishing')

相当于:update books_publisher set name='Apress Publishing' where id=52;

Publisher.objects.all (). Update (country = 'USA') // all values ​​becomes country USA

 

delete:

p = Publisher.objects.get(name = "Apress")

p.delete()

Publisher.objects.filter(name = 'Apress').delete()

Reproduced in: https: //www.cnblogs.com/stevenzeng/p/5059272.html

Guess you like

Origin blog.csdn.net/weixin_33881140/article/details/93881036