Transforming SQL Query to Django Expression - python

Assuming I have the following Django models and the according SQL-Tables with some data. I have simplified the models so that it is clearer.
UserAnswer:
class UserAnswer(models.Model):
partquestion = models.ForeignKey(PartQuestion)
answer = models.CharField()
id
answer
partquestion_id
1
667
1
PartQuestion:
class PartQuestion(models.Model):
question = models.ForeignKey(Question)
part = models.ForeignKey(Part)
id
question_id
part_id
1
1
1
Solution:
class SingleInputSolution(models.Model):
question = models.ForeignKey(Question)
content = models.CharField()
id
content
question_id
1
667
1
2
85
2
I want to get all User answers where the answer is equal to the solution of the according question. I've came up with this SQL-Query but can't really think of how to implement this into a Djano query:
select * from (useranswer join partquestion on
useranswer.partquestion_id = partquestion.id) join solution on
partquestion.question_id = solution.question_id where answer=content;
This will output the following:
useranswer.id
useranswer.answer
useranswer.partquestion_id
partquestion.id
partquestion.question_id
partquestion.part_id
solution.id
solution.content
solution.question_id
1
667
1
1
1
1
1
667
1
I just can't get my head around this concept in Django. Maybe using F-Expressions and some stuff.
Thanks for any advice.

You can just use values and the __ relations to traverse the foreign keys.
UserAnswer.objects.values('id', 'answer', 'partquestion_id', 'partquestion_set__id', 'partquestion_set__question_id')
Unfortunately it isn't obvious enough to me how to get those columns for the last table, but I hope that from this answer you can see how to do it?

Okay well I just figured it out myself after reading the answer and comments from #Swift.
It's:
UserAnswer.objects.filter(partquestion__question__solution__content=F('answer'))
It's pretty simple now after I got it but I just tried using solution_set instead of just solution.

If you define your models like this:
class UserAnswer(models.Model):
question = models.ForeignKey(questions,on_delete=models.CASCADE)
user_answer = models.CharField()
class questions(models.Model):
question = models.CharField()
answer = models.CharField()
then this filter must work
right_answers = UserAnswer.objects.filter(question__answer = user_answer )

Related

How to get highest value in django model for each objects

Admin wants to add different challenges. Each challenge has a lot of users. each user may have a lot of likes. I want to show the winner of each challenge. For that, I need to get which candidate gets the highest likes. How can I get it? is there any way like .count .?
how can I use that? in which model.
For example:
challenges
1 first_contest
2 second_contest
candidates
id name contest
1 jhon first_contest
2 sara second_contest
3 abi first_contest
candidates likes
id user_id candidate_id
1 1 1
2 2 2
3 1 1
In this case candidate, 1 = Jhon get 2 likes so in the first contest Jhon wins. Also in the second contest, Sara gets 1 like. So I need to show the winner in the first contest. How is that?
models.py:
class Challenge(models.Model):
title = models.CharField(max_length=50)
class Candidates(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
image = models.FileField( upload_to="challenge_candidates/",)
def likes_count(self):
return self.likes.all().count()
class CandidateLikes(models.Model):
like = models.CharField(max_length=10)
user =
models.ForeignKey(User,on_delete=models.CASCADE,related_name='candidate_likes')
contest_candidates = models.ForeignKey(Candidates, on_delete=models.CASCADE,
related_name='likes')
Sorry for my poor English. Thank you.
You first need to have a relationship between your CandidatLike model and Challenge model so that you can filter by challenge. A foreign key relation could be sufficient. Then you can add this query to your views
winner = CandidateLikes.objects.filter(challenge="your_challenge_name").order_by("like")
Notice that challenge should exist in your CandidatLike model, since we are filtering by it.
I think you are missing a relationship between Challenge and Candidates. Let's say you would add a challenge field to Candidate:
class Candidates(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
challenge = models.ForeignKey(Challenge, on_delete=models.CASCADE, related_name='candidates')
Then you can query the winner of each challenge with the highest like-count with a subquery like this:
from django.db.models import Count
from django.db.models import OuterRef, Subquery
cadidates = Candidates.objects.annotate(like_count=Count('likes')).filter(challange=OuterRef('pk')).order_by('-like_count')
queryset = Challenge.objects.annotate(winner=Subquery(cadidates.values('owner__username')[:1]))
This will give you a Challenge result query with an annotated winner like:
{'id': 1, 'title': 'Challange 1', 'winner': 'username'}

Perform JOIN in tables django

Perform JOIN on in django fetching related date in reverse relationship.
There are three models.
Following is code for models
class Question(models.Model):
title = models.CharField(max_length=255 )
description = models.TextField(max_length=300)
class Quiz(models.Model):
name = models.CharField(max_length=225,blank=False )
quiz_type =models.IntegerField(choices=QUIZ_TYPE,default=0)
questions = models.ManyToManyField( Question, through='QuestionQuiz', related_name="quiz_question")
categories= models.ManyToManyField(Category,through='CategoryQuiz',related_name='quiz_category')
class QuestionQuiz(models.Model):
quiz = models.ForeignKey(Quiz,on_delete=models.CASCADE)
question = models.ForeignKey(Question,on_delete=models.CASCADE)
correct =models.DecimalField(max_digits=4, decimal_places=3)
incorrect= models.DecimalField(max_digits=4, decimal_places=3)
class Meta:
unique_together = ('quiz','question')
In this the questions are added to the quiz using model Question Quiz.
Here , QuizQuestion has foreign key relationship with the question. I need to fetch all from question JOIN and records from QuestionQuiz with a particular quiz_id.
Suppose quiz_id =3
Then I will fetch all questions with correct and incorrect. if that quiz id is added to the question then it will display correct incorrect else these would be blank.
question_id | title|description|correct|incorrect|quesquizid
1 | Q1 |Q1desc |2 | -2 | 1
2 | Q2 |Q2desc | | |
ques_id =1 added to quiz_id=3 .ques_id=2 not added to quiz_id=3.So, correct incorrect are blank.
I tried following but it fetches all question and related quizes scores irrespective of their occurrence in current quiz :
Question.objects.prefetch_related('questionquiz_set').all()
The result should be similar to following query
Select * from question as qs LEFT JOIN questionquiz as qq on (qq.question_id = qs.id AND qq.id=3)
Please check the result of the query:
I think prefetch_related along with Prefetch may get you the desired result.
q = Question.objects.prefetch_related(
Prefetch('questionquiz_set', queryset=QuestionQuiz.objects.filter(quiz=3))
)
print(q)
This may retrieve all related data along with null if exist.

Django query: Joining two models with two fields

I have the following models:
class AcademicRecord(models.Model):
record_id = models.PositiveIntegerField(unique=True, primary_key=True)
subjects = models.ManyToManyField(Subject,through='AcademicRecordSubject')
...
class AcademicRecordSubject(models.Model):
academic_record = models.ForeignKey('AcademicRecord')
subject = models.ForeignKey('Subject')
language_group = IntegerCharField(max_length=2)
...
class SubjectTime(models.Model):
time_id = models.CharField(max_length=128, unique=True, primary_key=True)
subject = models.ForeignKey(Subject)
language_group = IntegerCharField(max_length=2)
...
class Subject(models.Model):
subject_id = models.PositiveIntegerField(unique=True,primary_key=True)
...
The academic records have list of subjects each with a language code and the subject times have a subject and language code.
With a given AcademicRecord, how can I get the subject times that matches with the AcademicRecordSubjects that the AcademicRecord has?
This is my approach, but it makes more queries than needed:
# record is the given AcademicRecord
times = []
for record_subject in record.academicrecordsubject_set.all():
matched_times = SubjectTime.objects.filter(subject=record_subject.subject)
current_times = matched_times.filter(language_group=record_subject.language_group)
times.append(current_times)
I want to make the query using django ORM not with raw SQL
SubjectTime language group has to match with Subject's language group aswell
I got it, in part thanks to #Robert Jørgensgaard Eng
My problem was how to do the inner join using more than 1 field, in which the F object came on handly.
The correct query is:
SubjectTime.objects.filter(subject__academicrecordsubject__academic_record=record,
subject__academicrecordsubject__language_group=F('language_group'))
Given an AcademicRecord instance academic_record, it is either
SubjectTime.objects.filter(subject__academicrecordsubject_set__academic_record=academic_record)
or
SubjectTime.objects.filter(subject__academicrecordsubject__academic_record=academic_record)
The results reflect all the rows of the join that these ORM queries become in SQL. To avoid duplicates, just use distinct().
Now this would be much easier, if I had a django shell to test in :)

Peewee ORM get similar entries based on foreign key

I'm having problem with writing query for getting similar posts in a blog based on tags they have. I have following models:
class Articles(BaseModel):
name = CharField()
...
class Tags(BaseModel):
name = CharField()
class ArticleTags(BaseModel):
article = ForeignKeyField(Articles, related_name = "articles")
tags = ForeignKeyField(Tags, related_name = "tags")
What i'd like to do is to get articles with similar tags sorted by amount of common tags.
Edit
After 2 hours of fiddling with it i got the anwser i was looking for, i'm not sure if it's the most efficient way but it's working:
Here is the function if anyone might need that in the future:
def get_similar_articles(self,common_tags = 1, limit = 3):
"""
Get 3 similar articles based on tag used
Minimum 1 common tags i required
"""
art = (ArticleTags.select(ArticleTags.tag)\
.join(Articles)\
.where(ArticleTags.article == self))
return Articles.select(Articles, ArticleTags)\
.join(ArticleTags)\
.where((ArticleTags.article != self) & (ArticleTags.tag << art))\
.group_by(Articles)\
.having(fn.Count(ArticleTags.id) >= common_tags)\
.order_by(fn.Count(Articles.id).desc())\
.limit(limit)
Just a stylistic nit, table names (and model classes) should preferably be singular.
# Articles tagged with 'tag1'
Articles.select().join(ArticleTags).join(Tags).where(Tags.name == 'tag1')

Django: Saving multiple ManyToMany fields within a transaction

this is the representation of my models:
class B(models.Model):
"""I'm a dummy model, so doesn't pay atention of what I do"""
name = models.CharField(max_length=250)
class A(models.Model):
name = models.CharField(max_length=250)
many_b = models.ManyToManyField(B)
Now, suppose I have a list of B objects. And a single A object that will be related to that Bs. Something like this:
a = A.objects.get(id=1)
list_of_b = [B<name='B1'>,B<name='B2'>,B<name='B3'>,]
The way I relate them now is this:
for b_object in list_of_b:
a.many_b.add(b_object)
Is there any way to add all the B objects in a single transaction? Maybe in a single method, like:
a.many_b.addList(b) #This doesn't exist
From the docs:
>>> john = Author.objects.create(name="John")
>>> paul = Author.objects.create(name="Paul")
>>> george = Author.objects.create(name="George")
>>> ringo = Author.objects.create(name="Ringo")
>>> entry.authors.add(john, paul, george, ringo)
So if you have a list, use argument expansion:
a.many_b.add(*list_of_b)
I guess what you want is a kind of bulk insert right?
As far as I know this is just available in the Django TRUNK not in 1.3!
check it out some tutorial:
http://www.caktusgroup.com/blog/2011/09/20/bulk-inserts-django/

Categories

Resources