Django's ORM is extremely powerful, allowing you to manage your data without ever going near a line of SQL and hiding a multitude of complexities. But its power can sometimes be a curse rather than a blessing, multiplying queries without your knowledge and bringing your database to its knees.
In this session I explain what's going on behind the scenes and present some techniques to make your ORM use more efficient, showing how to monitor what's going on and how to better deal with relationships, indexes and more.
This talk was presented at Europython 2010 in Birmingham.
YouTube Link: https://youtu.be/MsHqN-uJWqc
** RPA Training: https://www.edureka.co/robotic-process-automation-certification-courses **
This PPT on RPA Lifecycle will help you understand the different stages of building a bot with an example.
Following topics are covered in this session:
What is RPA?
RPA Lifecycle:
Discovery Phase
Solution Design Phase
Development Phase
UAT
Deployment Phase
Execute Bots
RPA Playlist: https://bit.ly/2B53HLe
RPA Blog Series: https://bit.ly/2Ay1bzG
Follow us to never miss an update in the future.
YouTube: https://www.youtube.com/user/edurekaIN
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
Castbox: https://castbox.fm/networks/505?country=in
Test Automation using UiPath Test Suite - Developer Circle Part-2.pdfDiana Gray, MBA
UiPath Test Suite Family of Products
In Part 2 of Test Automation using UiPath Test Suite - Developer Series, we'll cover,
- UiPath Test Manager: Manage tests with comprehensive test planning, defect reporting, role-based access controls, and requirement traceability
- UiPath Studio: Automate tests and test data generation by leveraging UiPath production-grade automation capabilities
UiPath Orchestrator: Distribute tests to schedule, monitor, and store dynamic test data and test execution & analyze detailed results
- UiPath Test Robots: Execute tests on multiple machines in parallel and receive reports back
- Guidance in setting the environment for Test Automation
Speakers: Atul Trikha , Sreenivasa Adathakula
YouTube Link: https://youtu.be/MsHqN-uJWqc
** RPA Training: https://www.edureka.co/robotic-process-automation-certification-courses **
This PPT on RPA Lifecycle will help you understand the different stages of building a bot with an example.
Following topics are covered in this session:
What is RPA?
RPA Lifecycle:
Discovery Phase
Solution Design Phase
Development Phase
UAT
Deployment Phase
Execute Bots
RPA Playlist: https://bit.ly/2B53HLe
RPA Blog Series: https://bit.ly/2Ay1bzG
Follow us to never miss an update in the future.
YouTube: https://www.youtube.com/user/edurekaIN
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
Castbox: https://castbox.fm/networks/505?country=in
Test Automation using UiPath Test Suite - Developer Circle Part-2.pdfDiana Gray, MBA
UiPath Test Suite Family of Products
In Part 2 of Test Automation using UiPath Test Suite - Developer Series, we'll cover,
- UiPath Test Manager: Manage tests with comprehensive test planning, defect reporting, role-based access controls, and requirement traceability
- UiPath Studio: Automate tests and test data generation by leveraging UiPath production-grade automation capabilities
UiPath Orchestrator: Distribute tests to schedule, monitor, and store dynamic test data and test execution & analyze detailed results
- UiPath Test Robots: Execute tests on multiple machines in parallel and receive reports back
- Guidance in setting the environment for Test Automation
Speakers: Atul Trikha , Sreenivasa Adathakula
From Event Receivers to SharePoint WebhooksAndré Vala
With SharePoint Online, Microsoft came up with Remote Event Receivers, a way to support the same use cases but keeping the code outside of SharePoint. Recently, Microsoft decided to embrace industry standards and added support for webhooks in SharePoint.
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
( Python Training : https://www.edureka.co/python )
This Edureka Python Class tutorial (Python Tutorial Blog: https://goo.gl/wd28Zr) will help you understand Python Classes and Objects with examples. It will also explain the concept of Abstract Classes and Inheritance in python.
Check out our Python Training Playlist: https://goo.gl/Na1p9G
This Python Programming tutorial video helps you to learn following topics:
1. Python Classes and Objects
2. Inheritance
3. Abstract Classes
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAVRohit malav
We will stick to the basic functionalities expected of a simple text editor – which includes the ability to – write something on the notepad, save it and open and modify it whenever required. For the purpose of this tutorial we will design the GUI in Tkinter.
Additionally we will use another standard python module called ScrolledText because the text widget module of Tkinter does not support scrolling functionality.
From Event Receivers to SharePoint WebhooksAndré Vala
With SharePoint Online, Microsoft came up with Remote Event Receivers, a way to support the same use cases but keeping the code outside of SharePoint. Recently, Microsoft decided to embrace industry standards and added support for webhooks in SharePoint.
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
( Python Training : https://www.edureka.co/python )
This Edureka Python Class tutorial (Python Tutorial Blog: https://goo.gl/wd28Zr) will help you understand Python Classes and Objects with examples. It will also explain the concept of Abstract Classes and Inheritance in python.
Check out our Python Training Playlist: https://goo.gl/Na1p9G
This Python Programming tutorial video helps you to learn following topics:
1. Python Classes and Objects
2. Inheritance
3. Abstract Classes
NOTEPAD MAKING IN PAYTHON BY ROHIT MALAVRohit malav
We will stick to the basic functionalities expected of a simple text editor – which includes the ability to – write something on the notepad, save it and open and modify it whenever required. For the purpose of this tutorial we will design the GUI in Tkinter.
Additionally we will use another standard python module called ScrolledText because the text widget module of Tkinter does not support scrolling functionality.
More and more web projects require interfacing with the backend using a REST-ful interface. In this presentation we'll discuss Django-REST-Framework's features and walk through how to integrate it into your next project!
An advanced forms presentation given with Miguel Araujo (marajop) at DjangoCon 2011. The transcript and slides is aimed at getting into Django Core, and Jacob Kaplan-Moss has stated this is his plan.
Full Stack & Full Circle: What the Heck Happens In an HTTP Request-Response C...Carina C. Zona
Presented at Confident Coding III, San Francisco, CA. October 20, 2012.
Flying in from 10,000 foot view (“Hey, browser, show me this”, “Okay, here it is”), we’ll take thoughtful overview of the HTTP request/response cycle. Its essence is simply a series of questions & answers, accumulating portions of content to be gracefully assembled for the user.
We’ll hone in on some key players amidst the “full stack” of communications, with special attention to how an understanding of the HTTP lifecycle endows any developer or designer with the power to optimize for performance, cost, and UX
There's plenty of material (documentation, blogs, books) out there that'll help
you write a site using Django... but then what? You've still got to test,
deploy, monitor, and tune the site; failure at deployment time means all your
beautiful code is for naught.
This session drills deep into the Amazon S3 technical best practices that help you maximize storage performance for your use case. We provide real-world examples and discuss the impact of object naming conventions and parallelism on Amazon S3 performance, and describe the best practices for multipart uploads and byte-range downloads.
Slides from our CodeMash 2013 Precompiler session, "Web Development with Python and Django", including a breezy introduction to the Python programming language and the Django web framework. The example code repository is available at https://github.com/finiteloopsoftware/django-precompiler/
This tutorial is very useful for freshers who is learning JAVA and hibernate. Which contains the following things like
mapping
association mapping
one to one
many to one
one to many
many many
mapping inheritance
concrete class
HQL
operation keywords
Joining Association
Fetch
This is a small introduction to the django framework I did for fellow engineers at Anevia. It touches on many of the major concepts and core features of django and gives a typical workflow built about the example of a minimalist blog engine.
This slideshow details how to create a means of searching a Django model without going outside of Django itself. No need for a full-text search engine, just use the power of Django.
In this talk, I am going to walk you through the building blocks for creating modern applications on Apple’s platforms using SwiftUI and Firebase.
We will cover the following topics:
- Setting up an architecture for data-driven applications
- What’s new in SwiftUI’s application life cycle
- Syncing data in realtime across multiple devices with Cloud Firestore
- Mapping Firestore documents in a type-safe way using the Codable protocol
- Using Combine to fetch data from remote APIs and Firebase
- How async/await (new in Swift 5.5) will affect the way we write asynchronous code (plus some surprises in Firebase's code base)
Firebase is always evolving, so I will also talk about how we’re making use of Apple’s latest technologies in our own code base, for example
- Combine
- async/await
- SwiftUI view modifiers
No matter if you’re a seasoned Firebase user or just wondering what it is all about, you should leave this session with a deeper understanding of what Firebase it and how you can use it your apps on Apple’s platforms.
Slides to the Hands On Spring Data lab, presented in Paris on Dec 10th, 2012. Code exercises are here: https://github.com/ericbottard/hands-on-spring-data
A Related Matter: Optimizing your webapp by using django-debug-toolbar, selec...Christopher Adams
This talk explains how to perform SQL query analysis and how to rewrite your views to reduce the number of queries Django uses in evaluating your model objects and their attributes. Special emphasis will be given to the powerful methods "select_related" and "prefetch_related." I will highlight the problem with a naive use of the ORM, how to target code for optimization, and the beneficial result.
Like any abstraction layer, the Django ORM hides the messy details about its underlying implementation. This is both the benefit and the risk. If used naively, any tool can cause unexpected or problematic outcomes. Likewise, the ORM can cause your application to interact with the database in an ugly and inefficient way, creating special challenges regarding scaling a quickly-prototyped webapp.
Many design patterns and best practices have been developed as a result to nudge developers to use the ORM more efficiently. The good news is, one of the easiest and most powerful patterns has been wrapped into Django itself, in the dual pairs of methods in the Django ORM's Queryset API, "select_related" and "prefetch_related." These methods instruct the Queryset, when evaluated, to perform two kinds of useful optimizations for you that can reduce the number of queries by orders of magnitude resulting from iterating over model objects and many-to-many relations.
This talk summarizes the problem these methods of the Queryset API try to solve, how to effectively use them, and the beneficial result. Mastering how to use Queryset methods efficiently and powerfully is a major step in moving from a beginner to intermediate Django developer.
Delivered at DjangoCon 2014.
Alfresco Content Modelling and Policy BehavioursJ V
Alfresco DevCon 2010 (Paris and New York)
This session starts by giving an overview of components of an Alfresco content model. We then examine the various forms of call-backs and hook-points available to the developer and give some examples of how these can be used to enforce custom business logic and model consistency.
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. What’s changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
UiPath Test Automation using UiPath Test Suite series, part 4DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 4. In this session, we will cover Test Manager overview along with SAP heatmap.
The UiPath Test Manager overview with SAP heatmap webinar offers a concise yet comprehensive exploration of the role of a Test Manager within SAP environments, coupled with the utilization of heatmaps for effective testing strategies.
Participants will gain insights into the responsibilities, challenges, and best practices associated with test management in SAP projects. Additionally, the webinar delves into the significance of heatmaps as a visual aid for identifying testing priorities, areas of risk, and resource allocation within SAP landscapes. Through this session, attendees can expect to enhance their understanding of test management principles while learning practical approaches to optimize testing processes in SAP environments using heatmap visualization techniques
What will you get from this session?
1. Insights into SAP testing best practices
2. Heatmap utilization for testing
3. Optimization of testing processes
4. Demo
Topics covered:
Execution from the test manager
Orchestrator execution result
Defect reporting
SAP heatmap example with demo
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...DanBrown980551
Do you want to learn how to model and simulate an electrical network from scratch in under an hour?
Then welcome to this PowSyBl workshop, hosted by Rte, the French Transmission System Operator (TSO)!
During the webinar, you will discover the PowSyBl ecosystem as well as handle and study an electrical network through an interactive Python notebook.
PowSyBl is an open source project hosted by LF Energy, which offers a comprehensive set of features for electrical grid modelling and simulation. Among other advanced features, PowSyBl provides:
- A fully editable and extendable library for grid component modelling;
- Visualization tools to display your network;
- Grid simulation tools, such as power flows, security analyses (with or without remedial actions) and sensitivity analyses;
The framework is mostly written in Java, with a Python binding so that Python developers can access PowSyBl functionalities as well.
What you will learn during the webinar:
- For beginners: discover PowSyBl's functionalities through a quick general presentation and the notebook, without needing any expert coding skills;
- For advanced developers: master the skills to efficiently apply PowSyBl functionalities to your real-world scenarios.
Elevating Tactical DDD Patterns Through Object CalisthenicsDorra BARTAGUIZ
After immersing yourself in the blue book and its red counterpart, attending DDD-focused conferences, and applying tactical patterns, you're left with a crucial question: How do I ensure my design is effective? Tactical patterns within Domain-Driven Design (DDD) serve as guiding principles for creating clear and manageable domain models. However, achieving success with these patterns requires additional guidance. Interestingly, we've observed that a set of constraints initially designed for training purposes remarkably aligns with effective pattern implementation, offering a more ‘mechanical’ approach. Let's explore together how Object Calisthenics can elevate the design of your tactical DDD patterns, offering concrete help for those venturing into DDD for the first time!
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
In today's fast-changing business world, Companies that adapt and embrace new ideas often need help to keep up with the competition. However, fostering a culture of innovation takes much work. It takes vision, leadership and willingness to take risks in the right proportion. Sachin Dev Duggal, co-founder of Builder.ai, has perfected the art of this balance, creating a company culture where creativity and growth are nurtured at each stage.
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 3. In this session, we will cover desktop automation along with UI automation.
Topics covered:
UI automation Introduction,
UI automation Sample
Desktop automation flow
Pradeep Chinnala, Senior Consultant Automation Developer @WonderBotz and UiPath MVP
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
2. About Me
• Python user for five years
• Discovered Django four years ago
• Worked full-time with Python/Django since
2008.
• Top Django answerer on StackOverflow!
• Occasionally blog on Django, concentrating
on efficient use of the ORM.
3. Contents
• Behind the scenes: models and fields
• How model relationships work
• More efficient relationships
• Other optimising techniques
9. Defining a model
• Model structure initialised via metaclass
• Called when model is first defined
• Resulting model class stored in cache to
use when instantiated
10. Fields
• Fields have contribute_to_class
• Adds methods, eg get_FOO_display()
• Enables use of descriptors for field access
12. Model instantiation
• Instance is populated from database initially
• Has no subsequent relationship with db
until save
• No identity between models
13. Querysets
• Model=manager returns a queryset:
foos Foo.objects.all()
• Queryset is an ordered list of instances
of a single model
• No database access yet
• Slice: foos[0]
• Iterate: {% for foo in foos %}
14. Where do all those
queries come from?
• Repeated queries
• Lack of caching
• Relational lookup
• Templates as well as views
15. Repeated queries
def get_absolute_url(self):
return "%s/%s" % (
self.category.slug,
self.slug
)
Same category, but query is
repeated for each article
16. Repeated queries
• Same link on every
page
• Dynamic, so can't
go in urlconf
• Could be cached
or memoized
19. Example models
class Foo(models.Model):
name = models.CharField(max_length=10)
class Bar(models.Model):
name = models.CharField(max_length=10)
foo = models.ForeignKey(Foo)
22. Fowards relationships
• Relational access implemented via a
descriptor:
django.db.models.fields.related.
SingleRelatedObjectDescriptor
• __get__ tries to access _foo_cache
• If doesn't exist, does lookup and creates
cache
23. select_related
• Automatically follows foreign keys in SQL
query
• Prepopulates _foo_cache
• Doesn't follow null=True relationships by
default
• Makes query more expensive, so be sure
you need it
24. Backwards relationships
{% for foo in my_foos %}
{% for bar in foo.bar_set.all %}
{{ bar.name }}
{% endfor %}
{% endfor %}
25. Backwards relationships
• One query per foo
• If you iterate over foo_set again, you
generate a new set of db hits
• No _foo_cache
• select_related does not work here
26. Optimising backwards
relationships
• Get all related objects at once
• Sort by ID of parent object
• Then cache in hidden attribute as with
select_related
27. qs = Foo.objects.filter(criteria=whatever)
obj_dict = dict([(obj.id, obj)
for obj in qs])
objects = Bar.objects.filter(foo__in=qs)
relation_dict = {}
for obj in objects:
relation_dict.setdefault(
obj.foo_id, []).append(obj)
for id, related in relation_dict.items():
obj_dict[id]._related = related
28. qs = Foo.objects.filter(criteria=whatever)
obj_dict = dict([(obj.id, obj)
for obj in qs])
objects = Bar.objects.filter(foo__in=qs)
relation_dict = {}
for obj in objects:
relation_dict.setdefault(
obj.foo_id, []).append(obj)
for id, related in relation_dict.items():
obj_dict[id]._related = related
29. qs = Foo.objects.filter(criteria=whatever)
obj_dict = dict([(obj.id, obj)
for obj in qs])
objects = Bar.objects.filter(foo__in=qs)
relation_dict = {}
for obj in objects:
relation_dict.setdefault(
obj.foo_id, []).append(obj)
for id, related in relation_dict.items():
obj_dict[id]._related = related
30. qs = Foo.objects.filter(criteria=whatever)
obj_dict = dict([(obj.id, obj)
for obj in qs])
objects = Bar.objects.filter(foo__in=qs)
relation_dict = {}
for obj in objects:
relation_dict.setdefault(
obj.foo_id, []).append(obj)
for id, related in relation_dict.items():
obj_dict[id]._related = related
31. qs = Foo.objects.filter(criteria=whatever)
obj_dict = dict([(obj.id, obj)
for obj in qs])
objects = Bar.objects.filter(foo__in=qs)
relation_dict = {}
for obj in objects:
relation_dict.setdefault(
obj.foo_id, []).append(obj)
for id, related in relation_dict.items():
obj_dict[id]._related = related
32. qs = Foo.objects.filter(criteria=whatever)
obj_dict = dict([(obj.id, obj)
for obj in qs])
objects = Bar.objects.filter(foo__in=qs)
relation_dict = {}
for obj in objects:
relation_dict.setdefault(
obj.foo_id, []).append(obj)
for id, related in relation_dict.items():
obj_dict[id]._related = related
33. Optimising backwards
[{'time': '0.000', 'sql': u'SELECT
"foobar_foo"."id", "foobar_foo"."name" FROM
"foobar_foo"'},
{'time': '0.000', 'sql': u'SELECT
"foobar_bar"."id", "foobar_bar"."name",
"foobar_bar"."foo_id" FROM "foobar_bar"
WHERE "foobar_bar"."foo_id" IN (SELECT
U0."id" FROM "foobar_foo" U0)'}]
34. Optimising backwards
• Still quite expensive, as can mean large
dependent subquery – MySQL in particular
very bad at these
• But now just two queries instead of n
• Not automatic – need to remember to use
_related_items attribute
35. Generic relations
• Foreign key to ContentType, object_id
• Descriptor to enable direct access
• iterating through creates n+m
queries(n=number of source objects,
m=number of different content types)
• ContentType objects automatically cached
• Forwards relationship creates _foo_cache
• but select_related doesn't work
36. generics = {}
for item in queryset:
generics.setdefault(item.content_type_id,
set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(
generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.
in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[content_type_id][item.object_id]
)
37. generics = {}
for item in queryset:
generics.setdefault(item.content_type_id,
set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(
generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.
in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[content_type_id][item.object_id]
)
38. generics = {}
for item in queryset:
generics.setdefault(item.content_type_id,
set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(
generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.
in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[content_type_id][item.object_id]
)
39. generics = {}
for item in queryset:
generics.setdefault(item.content_type_id,
set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(
generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.
in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[content_type_id][item.object_id]
)
40. generics = {}
for item in queryset:
generics.setdefault(item.content_type_id,
set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(
generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.
in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[content_type_id][item.object_id]
)
41. generics = {}
for item in queryset:
generics.setdefault(item.content_type_id,
set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(
generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.
in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[content_type_id][item.object_id]
)
42. generics = {}
for item in queryset:
generics.setdefault(item.content_type_id,
set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(
generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.
in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[content_type_id][item.object_id]
)
43. generics = {}
for item in queryset:
generics.setdefault(item.content_type_id,
set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(
generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.
in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[content_type_id][item.object_id]
)
44. generics = {}
for item in queryset:
generics.setdefault(item.content_type_id,
set()).add(item.object_id)
content_types = ContentType.objects.in_bulk(
generics.keys())
relations = {}
for ct, fk_list in generics.items():
ct_model = content_types[ct].model_class()
relations[ct] = ct_model.objects.
in_bulk(list(fk_list))
for item in queryset:
setattr(item, '_content_object_cache',
relations[content_type_id][item.object_id]
)
46. Memoizing
• Cache property on first access
• Can cache within instance, if multiple
accesses within same request
def get_expensive_items(self):
if not hasattr(self, '_cache'):
self._cache = self.expensive_op()
return self._cache
47. DB Indexes
• Pay attention to slow query log and
debug toolbar output
• Add extra indexes where necessary -
especially for multiple-column lookup
• Use EXPLAIN
48. Outsourcing
• Does all the logic need to go in the web
app?
• Services - via eg Piston
• Message queues
• Distributed tasks, eg Celery
49. Summary
• Understand where queries are coming
from
• Optimise where necessary, within Django
or in the database
• and...
(background: montage of Limmud, rosemanblog, Capital, Classic, Heart, GlassesDirect)
Some of same ideas in Guido's Appstats talk this morning
It's a model, in a field, geddit?
For more, see Marty Alchin, Pro Django (Apress)
descriptors used especially in related objects - see later
Very useful for introspection and working out what's going on
explain identity: multiple instances relating to same model row aren't the same object, changes made to one don't reflect the other; even saving one with new values won't be reflected in others.
Update, Aggregates, Q, F
Find repeated queries with my branch of the django-debug-toolbar, or SimonW's original query debug middleware
Actually in 1.2 there's an extra _state object in __dict__, which is used for the multiple DB support (which I'm not covering here).
Lack of model identity means that accessing the related item on one instance does not cause cache to be created on other instances that might reference the same db row
Note: backwards cache does work on OneToOne as of 1.2
+----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
| 1 | PRIMARY | sandy_bar | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
| 2 | DEPENDENT SUBQUERY | U0 | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using where |
+----+-----------+----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
| 1 | PRIMARY | sandy_bar | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
| 2 | DEPENDENT SUBQUERY | U0 | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using where |
+----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
--------+-----------+-----------------+---------------+---------+---------+------+------+-------------+