Hướng dẫn python if query is none - python nếu truy vấn không có
Tôi đã có một vấn đề tương tự khi tôi cần thực hiện nhiều truy vấn SQL. Vấn đề là một số truy vấn đã không trả về kết quả và tôi muốn in kết quả đó. Và đã có một sai lầm. Như đã viết, có một số giải pháp. Show
Cũng như:
Một trong những giải pháp là: Khối 1 và 2 cho phép bạn xử lý ________ 53/________ 54. Khối 5 cho phép bạn thực thi mã, bất kể kết quả của các khối 1 và 2. Vì vậy, vấn đề được trình bày có thể được giải quyết bằng cách sử dụng nó.
output:
Ví dụ ở trên chỉ trình bày sử dụng đơn giản như một ý tưởng có thể giúp giải pháp của bạn. Tất nhiên, bạn cũng nên chú ý đến các lỗi khác, chẳng hạn như tính chính xác của truy vấn, v.v. 10.5.8 & nbsp; mysqlcursor.fetchone () Phương thứcCú pháp:
Phương thức này lấy hàng tiếp theo của bộ kết quả truy vấn và trả về một chuỗi duy nhất hoặc 8 nếu không có thêm hàng. Theo mặc định, bộ tuple được trả về bao gồm dữ liệu được trả về bởi máy chủ MySQL, được chuyển đổi thành các đối tượng Python. Nếu con trỏ là một con trỏ thô, không có sự chuyển đổi như vậy xảy ra; Xem Phần & NBSP; 10.6.2, Lớp con trỏ.MysqlCursorraw.Phương thức 9 được sử dụng bởi fetchall () và fetchmany (). Nó cũng được sử dụng khi một con trỏ được sử dụng như một người lặp.Ví dụ sau đây cho thấy hai cách tương đương để xử lý kết quả truy vấn. Lần đầu tiên sử dụng 9 trong vòng lặp from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )1, thứ hai sử dụng con trỏ làm người lặp:
Bạn phải tìm nạp tất cả các hàng cho truy vấn hiện tại trước khi thực hiện các câu lệnh mới bằng cách sử dụng cùng một kết nối. Các biểu thức truy vấn mô tả một giá trị hoặc tính toán có thể được sử dụng như một phần của bản cập nhật, tạo, lọc, thứ tự theo, chú thích hoặc tổng hợp. Khi một biểu thức xuất ra giá trị boolean, nó có thể được sử dụng trực tiếp trong các bộ lọc. Có một số biểu thức tích hợp (được ghi lại bên dưới) có thể được sử dụng để giúp bạn viết các truy vấn. Biểu thức có thể được kết hợp, hoặc trong một số trường hợp lồng nhau, để tạo thành các tính toán phức tạp hơn. Số học được hỗ trợDjango hỗ trợ phủ định, cộng, phép trừ, nhân, chia, số học modulo và toán tử nguồn trên các biểu thức truy vấn, sử dụng hằng số python, biến và thậm chí các biểu thức khác. Vài ví dụ¶from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), ) Biểu thức tích hợpGhi chú Các biểu thức này được định nghĩa trong from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )2 và from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )3, nhưng để thuận tiện, chúng có sẵn và thường được nhập từ from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )4. from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), ) 5 Biểu thức JoLớp ________ 66¶from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )6¶ Một đối tượng from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )5 biểu thị giá trị của trường mô hình, giá trị biến đổi của trường mô hình hoặc cột được chú thích. Nó có thể đề cập đến các giá trị trường mô hình và thực hiện các hoạt động cơ sở dữ liệu bằng cách sử dụng chúng mà không thực sự phải kéo chúng ra khỏi cơ sở dữ liệu vào bộ nhớ Python. Thay vào đó, Django sử dụng đối tượng from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )5 để tạo biểu thức SQL mô tả hoạt động cần thiết ở cấp cơ sở dữ liệu. Hãy để thử điều này với một ví dụ. Thông thường, người ta có thể làm điều gì đó như thế này: # Tintin filed a news story! reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed += 1 reporter.save() Ở đây, chúng tôi đã kéo giá trị của from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )9 từ cơ sở dữ liệu vào bộ nhớ và thao tác nó bằng các toán tử python quen thuộc, sau đó lưu đối tượng trở lại cơ sở dữ liệu. Nhưng thay vào đó chúng ta cũng có thể làm được: from django.db.models import F reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed = F('stories_filed') + 1 reporter.save() Mặc dù # Tintin filed a news story! reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed += 1 reporter.save()0 trông giống như một gán giá trị Python bình thường cho một thuộc tính thể hiện, nhưng trên thực tế, nó là một cấu trúc SQL mô tả một hoạt động trên cơ sở dữ liệu. Khi Django gặp một thể hiện from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )5, nó sẽ ghi đè các toán tử Python tiêu chuẩn để tạo biểu thức SQL được đóng gói; Trong trường hợp này, một trong đó hướng dẫn cơ sở dữ liệu để tăng trường cơ sở dữ liệu được biểu thị bằng from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )9. Bất kể giá trị nào là hoặc trên from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )9, Python không bao giờ biết về nó - nó được xử lý hoàn toàn bằng cơ sở dữ liệu. Tất cả các Python, thông qua lớp Django từ ____ ____65, là tạo cú pháp SQL để tham khảo trường và mô tả hoạt động. Để truy cập giá trị mới được lưu theo cách này, đối tượng phải được tải lại: reporter = Reporters.objects.get(pk=reporter.pk) # Or, more succinctly: reporter.refresh_from_db() Cũng như được sử dụng trong các hoạt động trên các trường hợp đơn lẻ như trên, from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )5 có thể được sử dụng trên # Tintin filed a news story! reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed += 1 reporter.save()6 của các trường hợp đối tượng, với # Tintin filed a news story! reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed += 1 reporter.save()7. Điều này làm giảm hai truy vấn mà chúng tôi đã sử dụng ở trên - # Tintin filed a news story! reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed += 1 reporter.save()8 và # Tintin filed a news story! reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed += 1 reporter.save()9 - chỉ một: 0Chúng ta cũng có thể sử dụng # Tintin filed a news story! reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed += 1 reporter.save()7 để tăng giá trị trường trên nhiều đối tượng - có thể nhanh hơn nhiều so với việc kéo tất cả chúng vào Python từ cơ sở dữ liệu, lặp lại chúng, tăng giá trị trường của từng con và lưu từng cái trở lại cơ sở dữ liệu : 1from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )5 Do đó có thể cung cấp các lợi thế về hiệu suất bằng cách:
Tránh các điều kiện chủng tộc bằng cách sử dụng ____ ____ 65¶Một lợi ích hữu ích khác của from django.db.models import Count, F, Value from django.db.models.functions import Length, Upper from django.db.models.lookups import GreaterThan # Find companies that have more employees than chairs. Company.objects.filter(num_employees__gt=F('num_chairs')) # Find companies that have at least twice as many employees # as chairs. Both the querysets below are equivalent. Company.objects.filter(num_employees__gt=F('num_chairs') * 2) Company.objects.filter( num_employees__gt=F('num_chairs') + F('num_chairs')) # How many chairs are needed for each company to seat all employees? >>> company = Company.objects.filter( ... num_employees__gt=F('num_chairs')).annotate( ... chairs_needed=F('num_employees') - F('num_chairs')).first() >>> company.num_employees 120 >>> company.num_chairs 50 >>> company.chairs_needed 70 # Create a new company using expressions. >>> company = Company.objects.create(name='Google', ticker=Upper(Value('goog'))) # Be sure to refresh it if you need to access the field. >>> company.refresh_from_db() >>> company.ticker 'GOOG' # Annotate models with an aggregated value. Both forms # below are equivalent. Company.objects.annotate(num_products=Count('products')) Company.objects.annotate(num_products=Count(F('products'))) # Aggregates can contain complex computations also Company.objects.annotate(num_offerings=Count(F('products') + F('services'))) # Expressions can also be used in order_by(), either directly Company.objects.order_by(Length('name').asc()) Company.objects.order_by(Length('name').desc()) # or using the double underscore lookup syntax. from django.db.models import CharField from django.db.models.functions import Length CharField.register_lookup(Length) Company.objects.order_by('name__length') # Boolean expression can be used directly in filters. from django.db.models import Exists Company.objects.filter( Exists(Employee.objects.filter(company=OuterRef('pk'), salary__gt=10)) ) # Lookup expressions can also be used directly in filters Company.objects.filter(GreaterThan(F('num_employees'), F('num_chairs'))) # or annotations. Company.objects.annotate( need_chairs=GreaterThan(F('num_employees'), F('num_chairs')), )5 là việc có cơ sở dữ liệu - thay vì Python - cập nhật giá trị trường Lĩnh vực tránh được điều kiện cuộc đua. Nếu hai luồng Python thực thi mã trong ví dụ đầu tiên ở trên, một luồng có thể truy xuất, tăng và lưu giá trị trường sau khi tên kia đã lấy nó từ cơ sở dữ liệu. Giá trị mà luồng thứ hai tiết kiệm sẽ dựa trên giá trị ban đầu; Công việc của chủ đề đầu tiên sẽ bị mất. Nếu cơ sở dữ liệu chịu trách nhiệm cập nhật trường, quá trình này mạnh hơn: nó sẽ chỉ cập nhật trường dựa trên giá trị của trường trong cơ sở dữ liệu khi # Tintin filed a news story! reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed += 1 reporter.save()9 hoặc # Tintin filed a news story! reporter = Reporters.objects.get(name='Tintin') reporter.stories_filed += 1 reporter.save()7 được thực thi, thay vì dựa trên giá trị của nó khi ví dụ đã được lấy.
|