I recently found that too much SQL query optimization issue. django-debug-tool reported hundreds of similar and duplicate queries. So, I'm trying to figure out the best efficiency of Django ORM to avoid unnecessary Queryset evaluation.
As you see the below Store model, a Store model has many Foreign key and ManyToManyFields. Due to that structure, there are many code snippets doing the blow on HTML template files such as store.image_set.all or store.top_keywords.all. Everything starts with store. In each store detail page, I simply pass a cached store object with prefetch_related or select_related. Is this a bad approach? Should I cache and prefetch_related or select_related each Foreign key or ManyToManyField separately on views.py?
HTML templates
{% for img in store.image_set.all %}
{{ img }}
{% endfor %}
{% for top_keyword in store.top_keywords.all %}
{{ top_keyword }}
{% endfor %}
{% for sub_keyword in store.sub_keywords.all %}
{{ sub_keyword }}
{% endfor %}
views.py
class StoreDetailView(View):
def get(self, request, *args, **kwargs):
cache_name_store = 'store-{0}'.format(store_domainKey)
store = cache.get(cache_name_store, None)
if not store:
# query = get_object_or_404(Store, domainKey=store_domainKey)
query = Store.objects.all().prefetch_related('image_set').get(domainKey=store_domainKey)
cache.set(cache_name_store, query)
store = cache.get(cache_name_store)
context = {
'store': store,
}
return render(request, template, context)
models.py
class Store(TimeStampedModel):
categories = models.ManyToManyField(Category, blank=True)
price_range = models.ManyToManyField(Price, blank=True)
businessName = models.CharField(unique=True, max_length=40,
verbose_name='Business Name')
origin = models.ForeignKey(Origin, null=True, on_delete=models.CASCADE, blank=True)
ship_to = models.ManyToManyField(ShipTo, blank=True)
top_keywords = models.ManyToManyField(Keyword, blank=True, related_name='store_top_keywords')
sub_keywords = models.ManyToManyField(SubKeyword, blank=True, related_name='store_sub_keywords')
sponsored_stores = models.ManyToManyField(
'self', through='Sponsorship', symmetrical=False, related_name='sponsored_store_of_store')
similar_stores = models.ManyToManyField(
'self', through='Similarity', symmetrical=False, related_name='similar_store_of_store')
shortDesc = models.TextField(blank=True, verbose_name='Short Description')
longDesc = models.TextField(blank=True, verbose_name='Long Description')
returnPol = models.TextField(verbose_name='Return Policy', blank=True)
returnUrl = models.CharField(max_length=255, null=True, blank=True, verbose_name='Return Policy URL')
likes = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, editable=False)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, editable=False, on_delete=models.CASCADE,
related_name='stores_of_created_by', null=True, blank=True)
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, editable=False, on_delete=models.CASCADE,
related_name='stores_of_updated_by', null=True, blank=True)
I really wouldn't advise custom caching/performance optimisation, unless it's a very last resort. Django has great docs on querysets and optimisation - if you follow those, it should be rare for you to experience major performance issues that require custom workarounds.
I think the issue here is that you're printing your objects in a template and hence calling their str() method. There's nothing wrong with this, but I'd check what variables you're using in your str() methods. I suspect you're referencing other models? I.e. the str() method in your image model (or whatever) is doing something like image.field.other_field. In this case, your query should look like:
queryset = Store.objects.prefetch_related('image_set__field')
Your final queryset may look like:
queryset = Store.objects.prefetch_related('image_set__field1', 'image_set__field2', 'top_keywords__field3', ...)
Note that you can still pass this into get_object_or_404 like so:
get_object_or_404(queryset, pk=<your_stores_id>)
Hope this helps.
Related
Here i have a Model Recommenders:
class Recommenders(models.Model):
objects = None
Subject = models.ForeignKey(SendApproval, on_delete=models.CASCADE, null=True)
Recommender = models.CharField(max_length=20, null=True)
Status = models.CharField(null=True, max_length=8, default="Pending")
Time = models.DateTimeField(auto_now_add=True)
And another model Approvers:
class Approvers(models.Model):
objects = None
Subject = models.ForeignKey(SendApproval, on_delete=models.CASCADE, null=True)
Approver = models.CharField(max_length=20, null=True)
Status = models.CharField(null=True, max_length=8, default="Pending")
Time = models.DateTimeField(auto_now_add=True)
And my SendApproval model as:
class SendApproval(models.Model):
Subject = models.CharField(max_length=256)
Date = models.DateField(null=True)
Attachment = models.FileField(upload_to=get_file_path)
SentBy = models.CharField(null=True, max_length=100)
Status = models.CharField(null= True, max_length=8, default="Pending")
Now my problem is that I have to display the Subject and Attachment from SendApproval table only when all the recommender's Status in Recommenders table related to that subject is "Approved"
Don't know how can I know that...Thanks in advance...
Actually not have any Idea about the solution but the best answer will be appreciated...By the way, I am new to StackOverflow...So please let me know if there is some ambiguity in my question.
Offer two ways.
1.Here the Recommenders model is filtered by the Status='Approved' field. By the Subject field, which is associated with the primary model, I get access to the SendApproval fields.
Replace bboard with the name of the folder where your templates are placed.
I have this: templates/bboard which are in the application folder.
views.py
def info(request):
recomm = Recommenders.objects.filter(Status='Approved')
return render(request, 'bboard/templ.html', {'context': recomm})
urls.py
urlpatterns = [
path('info/', info, name='info'),
]
templates
{% for a in context %}
<p>{{ 'Subject' }} : {{ a.Subject.Subject }} {{ 'Attachment' }} : Link
{{ 'id SendApproval' }} : {{ a.Subject.id }}</p>
{% endfor %}
2.You can also filter by primary model by fields from a secondary model that is linked to the primary. Here, I passed in the filter the name of the model in lower case and the search field.
In the primary model, by default, a property is created to refer to the secondary model, this is the name of the model in lower case and the _set prefix. In the outer loop, all received rows of the primary model are sorted out, and in the inner loop, through recommenders_set.all(), we get the Status. As you can see it is Approved.
sa = SendApproval.objects.filter(recommenders__Status='Approved')
print(sa)
for i in sa:
for k in i.recommenders_set.all():
print('status', k.Status)
I have a profile, and in this profile I want to display bookmarks for all messages (this is my IntegerField). In other words, how many people have bookmarked a particular author's posts.
models.py
class Post(models.Model):
slug = models.SlugField(unique=True)
title = models.CharField(max_length=255, db_index=True)
author = models.ForeignKey(
"users.CustomUser", on_delete=models.SET_NULL, null=True, db_index=True
)
bookmarkscount = models.IntegerField(null=True, blank=True, default=0)
class Profile(models.Model):
user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE)
This is my try in template but it does not work
<p>Bookmark</p>
<p>{{posts.bookmarkscount}}</p>
But work only if I use "for"
{% for post in posts %}
<p>{{ post.bookmarkscount}}</p>
{% endfor %}
views.py
class ProfileDetailView(DetailView):
model = Profile
template_name = "users/profile/profile.html"
def get_context_data(self, **kwargs):
try:
context["posts"] = Post.objects.filter(
author=self.object.user.is_authenticated
)
except Post.DoesNotExist:
context["posts"] = None
posts is a QuerySet type, a representation of the query to be sent to the DB. More like a list on steroids rather than a single instance of Post. This is a crucial concept you need to understand before coding anything in Django. (Docs here)
In order to get a sum of all the bookmarkscount values from all posts of a user, you need to use aggregation. (Docs here)
from django.db.models import Sum
posts.aggregate(Sum('bookmarkscount'))
# returns i.e.: {'bookmarkscount': 234}
I have 2 models: Post and Comment. Like this:
class Post(models.Model):
content = models.TextField()
class Comment(models.Model)
content = models.TextField()
owner = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
post_parent = models.ForeignKey(
Post,
on_delete=models.CASCADE,
null=False,
db_column='post_parent',
related_name='comments',
)
comment_ref = models.ForeignKey(
'self',
on_delete=models.CASCADE,
null=True,
related_name='children_comment'
)
Suppose that comments can have sub-comments in 1 level. With a post's ID, I want to get 5 newest comments (not sub-comment), each of them is prefetched with their 3 newest sub-comments.
My current implementation after searching is:
sub_comments = Comment.objects.filter(
comment_ref=OuterRef('pk'),
post_parent=post_id
).order_by('-timestamp').values_list('id', flat=True)[:3]
queryset = Comment.objects.filter(
comment_ref=None,post_parent=post_id
).select_related('owner').prefetch_related(
Prefetch('children_comment', queryset=Comment.objects.filter(
id__in=Subquery(sub_comments)
), to_attr="cmt"))
That does not work. It doesn't limit the number of prefetched sub-comments. Please help me :D
You will need a date field on the Comment table so you can show the latest comments and sub comments.
I would introduce another model here so its easier for queries and writing tests personally for me.
Models.py
class Post(models.Model):
content = models.TextField()
class Comment(models.Model)
content = models.TextField()
owner = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
post_parent = models.ForeignKey(
Post,
on_delete=models.CASCADE,
null=False,
db_column='post_parent',
related_name='comments',
)
date_posted = models.DateTimeField()
class ChildComment(models.Model):
parent_comment = models.ForeignKey(
Comment,
on_delete=models.CASCADE,
null=True,
related_name="child_comments"
)
Views.py
post = Post.objects.get(id=<some_id>)
comments = Comment.objects.filter(
post_parent_id=post.id
).prefetch_related(
'child_comments'
).order_by(
'-date_posted'
)[:5]
recent_comments = {}
for comment in comments:
recent_comments[comment] = [
comment.child_comments.all().order_by(
'-parent_comment__date_posted'
)[:3]
]
Pass recent_comments to the template context and then in a template file use it like:
{% for recent_comment, child_comments in recent_comments.items %}
{{ recent_comment.content }}
{% for child_comment in child_comments %}
{{ child_comment.content }}
{% endfor %}
{% endfor %}
You will need a DateTimeField to get the latest comments and child comments in an order_by, but this query should work. This may not be ideal if you were returning thousands of comments, but only 5 at a time, this will be fast.
comments = Comment.objects.filter(
post_parent_id=post.id
)[:5]
root_comment_ids = [comment.id for comment in comments]
child_comments = Comment.objects.filter(
comment_ref_id__in=root_comment_ids
)[:3]
I am building a wiki and need to save every revision made for each wikipage. This means that i need a new revision tabel for every wikipage created.
When presenting each wikipage template with DetailView i need to access Wikipage.title, the latest revision and its Revision.content, Revision.author, Revision.last_edit and Revision.comment. I have been able to access the title, by setting "model=Wikipage" and revision, by setting "model=Revision" but not both at the same time.
models.py
class Wikipage(models.Model):
title = models.CharField(max_length=100)
date_created = models.DateTimeField('Created', auto_now_add=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = "Wikipages"
class Revision(models.Model):
wikipage = models.ForeignKey(Wikipage, null=True,
on_delete=models.CASCADE, related_name='revision')
content = models.TextField('Content')
author = models.ForeignKey(User, null=True,
on_delete=models.SET_NULL)
last_edit = models.DateTimeField('Last edit', auto_now=True)
comment = models.TextField('Comment', blank=True)
class Meta:
verbose_name = 'Revision'
verbose_name_plural = 'Revisions'
ordering = ['-last_edit']
get_latest_by = ['last_edit']
def __str__(self):
return self.content
I wanted to use the DetailView and CreateView that comes with django, but I have not succeeded in accessing specific data from both tables.
I have gotten the ListView to work correctly, but that only needs the title from Wikipage, and nothing from Revision.
You can access the latest revision for a wikipage via wikipage.revision.latest(), since you correctly defined get_latest_by on the Revision model. You can do that directly in the template:
{% with wikipage.revision.latest as revision %}
{{ revision.last_edit }}
{{ revision.comment }}
{% endwith %}
On the DetailView you can access all the revisions using wikipage.revision where wikipage is the object of the DetailView and you could query the revisions to get the latest one.
I would recommend also this change
wikipage = models.ForeignKey(Wikipage, null=True, on_delete=models.CASCADE, related_name='revision')
to be
wikipage = models.ForeignKey(Wikipage, null=True, on_delete=models.CASCADE, related_name='revisions')
I have 2 models -
class InsName(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=30, verbose_name = "Insurer/Broker")
alias = models.TextField(max_length=80, blank=True)
def __str__(self):
return f'{self.name}, {self.alias}'
def get_absolute_url(self):
return reverse('insurer-detail', args=[str(self.id)])
class Development(models.Model):
id = models.AutoField(primary_key=True)
logno = models.CharField(validators=[RegexValidator(regex='^(SCTASK|CDLI)[0-9]{7}', message='Please enter a valid log number', code='nomatch')], max_length=13)
insurer = models.ForeignKey(InsName, on_delete=models.SET_NULL, null=True, blank=False, verbose_name="Client")
policy = models.ManyToManyField(Policy, blank=True)
on my template I am outputting a list of Developments but where insurer is output I just want the name part to output. I need to retain the alias as it is used in other templates that also calls in InsName.
I thought I could use a substring before comma method in the template but I cant see that such a thing exists. Is this possible? If not any tips on how I can achieve this is greatly appreciated!
Maybe you can do it like this using F (apart from comment of #dirkgroten):
queryset = Development.objects.all().annotate(insurer_name=F('insurer__name'))
And use it in template:
{% for item in queryset %}
{{ item.insurer_name }}
{% endfor %}