The single database table operation djang

13 will be operating Summary:

Query method:

<1> all (): Search Results
<2> filter (** kwargs) : it contains the object to match the filter of
<3> get (** kwargs) : returns the given match the filter It objects, and only one result is returned, if the object matching the filter will throw more than one or no errors.
<4> exclude (** kwargs) : it contains the given object does not match the filter criteria of
<5> order_by (* field) : order query results ( '-id') / ( '. Price')

<. 6> reverse (): reverse sort query results earlier >>> first have to reverse the sort
<7> count (): returns the database that match the query (QuerySet) the number of objects.
<8> first (): returns the first record
<9> last (): returns the last record
<10> exists (): if QuerySet contains data, returns True, otherwise return False
<. 11> values (* Field) : returns a ValueQuerySet-- QuerySet that a special, not obtained after running a series of instantiating an object model, but a dictionary iterative sequence
<12> values_list (* field) : it values () are very similar, it returns a tuple sequence, values returns a dictionary sequence
<13> distinct (): discarding duplicates from the returned results History

 

Return QuerySet object methods

all()

filter()

exclude()

order_by()

reverse()

distinct()

Special QuerySet

values ​​() Returns an iterative sequence dictionaries

values_list () returns an ancestral sequence of iterations

 

Return specific object

get()

first()

last()

Returns a Boolean value methods are:

exists()

Returns the methods

count()

 

example:

models:

from django.db import models

# Create your models here.

class User(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    register_time = models.DateField()

    def __str__(self):
        return "对象的名字%s" % self.name
View Code

 

Single-table operation

from django.test Import TestCase 

# the Create your Tests here Wallpaper. 
Import os
 Import SYS 

IF  __name__ == " __main__ " : 
    os.environ.setdefault ( " DJANGO_SETTINGS_MODULE " , " untitled_orm.settings " )
     Import Django 
    django.setup () 
    from app01 Import Models 

    # new data objects can be as long as queryset unlimited point queryset method 
    # models.User.objects.filter () filter () COUNT ().. 


    # based on create to create - 
    # user_obj = models.User.objects.create(name='egon1', age=38, register_time="2017-7-1")
    # print(user_obj.register_time)

    # 基于对象的绑定方法创建二
    # user_obj = models.User(name='Kevin', age=30, register_time='2019-1-1')
    # user_obj.save()

    # from datetime import datetime
    # ctime = datetime.now()
    # models.User.objects.create(name='egon', age=18, register_time=ctime)


    # 修改数据
    # user_obj = models.User.objects.filter(name='jason')
    # print(user_obj)  # <QuerySet [<User: User object>, <User: User object>]>

    # 基于对象的
    # user_obj = models.User.objects.filter(name='jason').first()
    # print(user_obj)  # User object
    # user_obj.age = 18
    # user_obj.save()

    # 基于queryset
    # models.User.objects.filter(name='Kevin').update(name='kevin', age=66)


    # 删除数据
    # 基于queryset
    # res = models.User.objects.filter(name='egon')
    # print(res)
    # models.User.objects.filter(name='egon').delete()

    # 基于对象
    # user_ob = models.User.objects.filter(name='owen').first()
    # user_ob.delete()

    # 查询数据
    #1. all (): Search Results QuerySet that [obj, obj] 
    # 2. filter (** kwargs): it contains the objects that match the filter conditions given QuerySet that [obj, obj] 
    # RES = models.User.objects .filter (name = 'jason', age = 18) can be placed within a plurality of constraints but note # filter yes and the relationship between a plurality of conditions 

    # 3. GET (** kwargs) Object 
    # returns to the objects that match the filter criteria, and only one result is returned, if the object matching the filter will throw more than one or no errors 
    # RES = models.User.objects.get (name = 'Jason') # is not recommended 
    # Print (RES) 

    # 4.exclude (** kwargs): <QuerySet that [<the User: the User Object>, <the User: the User Object>, <the User: the User Object>]> 
    # which contains the mismatch and filters to object 
    # RES = models.User.objects.exclude (name = 'Jason') 
    # Print (RES) 

    #5.order_by (* field): order query results QuerySet that [obj, obj] 
    # RES = models.User.objects.order_by ( 'Age') ascending # default 
    # Print (RES) 

    # RES = models.User.objects. ORDER_BY ( '- Age') 
    # Print (RES) descending # 

    # RES = models.User.objects.order_by ( 'name') 
    # Print (RES) 

    # 6. The Reverse () QuerySet that [obj, obj] query results trans ordered phase, with a first sort order to reverse the front 
    # RES = models.User.objects.order_by ( 'name'). reverse () 
    # Print (RES) 

    # 7. the COUNT () returns the database match query 
    # RES = Models .User.objects.count () 
    # or 
    # RES = models.User.objects.all (). COUNT () 
    # Print (RES) 

    #8.first (): returns the first record obj 
    # RES = models.User.objects.all () First (). 
    # Print (RES) 

    # RES = models.User.objects.all () [0] # is not the value of the index supports negative 
    # Print (RES) 

    # 9.last (): returns the last data obj 
    # RES = models.User.objects.all () last (). 
    # Print (RES) 

    # 10. the EXISTS () : If QuerySet contains data, returns True, otherwise return False 
    # RES = models.User.objects.all () EXISTS (). 
    # RES1 = models.User.objects.filter (name = 'Jason', Age =. 3) .exists () 
    # Print (RES, res1) 

    # 11. values (Field, *): returns a QuerySet --- - a special QuerySet, 
    # after a series of model runs have is not an instance of an object, but a iterable dictionary 
    #res = models.User.objects.values ( 'name', 'age') # QuerySet object dictionary list sets 
    # Print (RES) # <QuerySet that [{ 'name': 'Jason', 'Age':} 18 is, { 'name': 'Aegon1', 'Age': 38 is}]> 

    # 12. The values_list (): returns a set of tuples QuerySet list 
    # RES = models.User.objects.values_list ( 'name', 'Age') 
    # Print (RES) 
    # <QuerySet that [( 'Jason', 18 is), ( 'Nick', 44 is), ( 'Kevin', 66),] 

    # 13.distincat (): delete duplicate records from the returned results, the to return the result of re-out, 

    # RES = models.User.objects.distinct () # QuerySet that [<the User: name of the object jason>, <User: object name Nick>,] 
    # Print (RES) 
    # RES = models.User.objects.values ( 'name', 'age')
    #res1 = models.User.objects.values ( 'name', 'age'). distinct () # [front to heavy objects must be identical to the data de-duplication] 

    # 14. magic double underlined query <QuerySet [ obj, obj] 
    # query older than 44 years old 
    # RES = models.User.objects.filter (= 34 is age__gt) 
    # Print (RES) 

    # user query younger than 44 years of age <QuerySet [<user: object name jason >, <User: object name tank>, <User: moniker Aegon1>, <User: the object name Jason>]> 
    # RES = models.User.objects.filter (= 44 is age__lt) 
    # Print (RES) 

    # query older than 44 years old 
    # RES = models.User.objects.filter (age__gte = 44) 
    # Print (RES) 

    # query the user name contains n 
    # RES = models.User.objects.filter (name__contains = ' n ')
    #
    #res = models.User.objects.filter (name__icontains = 'n ') # ignore case 
    # Print (RES) 

    # query user names beginning with j 
    # RES = models.User.objects.filter (name__istartswith = 'j') 
    # Print (RES) 

    # query time is registered in the 2017 user 
    # RES = models.User.objects.filter (register_time__year = 2017) # SQLite format will be screened to date with the Create 
    # Print (RES)
View Code

 

Guess you like

Origin www.cnblogs.com/qingqinxu/p/11258665.html