I have a serious problem, I need to retrieve a query set from an unrelated model
my model:
# problem/models.py
User = get_user_model()
class Problem(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length=1000)
writer = models.ForeignKey(User,models.CASCADE)
score = models.PositiveIntegerField(default=100) # my perfer is PositiveSmallIntegerField
class Submission(models.Model):
submitted_time = models.DateTimeField(auto_now_add=True)
participant = models.ForeignKey(User,related_name="submissions",on_delete=models.CASCADE)
problem = models.ForeignKey(Problem,related_name="submissions",on_delete=models.CASCADE)
code = models.URLField(max_length=200)
score = models.PositiveIntegerField(default=0)
# contest/models.py
class Contest(models.Model):
name = models.CharField(max_length=50)
holder = models.ForeignKey(User, on_delete=models.CASCADE)
start_time = models.DateTimeField()
finish_time = models.DateTimeField()
is_monetary = models.BooleanField(default=False)
price = models.PositiveIntegerField(default=0)
problems = models.ManyToManyField(Problem)
authors = models.ManyToManyField(User, related_name='authors')
participants = models.ManyToManyField(User, related_name='participants')
I trying to retrieve a query set of Submission like this :
<QuerySet [<Submission: Submission object (12)>, <Submission: Submission object (6)>, <Submission: Submission object (16)>, <Submission: Submission object (5)>, <Submission: Submission object (11)>, <Submission: Submission object (15)>, <Submission: Submission object (14)>, <Submission: Submission object (4)>, <Submission: Submission object (10)>, <Submission: Submission object (9)>, <Submission: Submission object (3)>, <Submission: Submission object (8)>, <Submission: Submission object (2)>, <Submission: Submission object (13)>, <Submission: Submission object (1)>, <Submission: Submission object (7)>]>
with the id of the Contest model , but I don't know how it is possible
CodePudding user response:
If I'm understanding your question correctly, what you're trying to do isn't possible in your current code. If you want to pull back all Submission
objects filtered by a specific Contest
id, you'll need to connect the two.
For instance, here's an example of what the call should be:
submissions_by_contest = Submission.object.filter(contest.id=123) # or whatever the id/pk field is on Contest
However, in order for this to work, you'll have to modify the Submission
model to something like this:
from contest.models import Contest
class Submission(models.Model):
submitted_time = models.DateTimeField(auto_now_add=True)
participant = models.ForeignKey(User,related_name="submissions",on_delete=models.CASCADE)
problem = models.ForeignKey(Problem,related_name="submissions",on_delete=models.CASCADE)
code = models.URLField(max_length=200)
score = models.PositiveIntegerField(default=0)
contest = models.ForeignKey(Contest,related_name="submissions")
This will connect the Submission
model back to Contest
as contest
.
You may have to modify this a bit based on where your files are and what sub-directories are pythonic modules, but it should generally get you there.
EDIT: (based on context from reply)
Since you can't edit the initial code, you'll do something like this instead:
You have a connection that goes from Contest (m2m) to Problem and another that goes from Submission back to Problem. That's what you'll use.
First, you have the contest name or id or some identifier. You'll get that object.
contest = Contest.objects.get(name="contest name") # or id
Then, you'll grab all of the Submissions that are in the contest.problems
. Something like:
submissions_by_contest = Submission.objects.filter(problem__in=contest.problems)
Syntax may differ slightly, but that's the connection you'd use.