Home > Mobile >  How to get a Queryset from a list of IDs without using loop?
How to get a Queryset from a list of IDs without using loop?

Time:12-13

I want to get data from the database using a list of IDs. I have a model.

class Point(models.Model):
    p1 = models.IntegerField()
    p2 = models.IntegerField()

This model has bunch of data in my database. I have a list of IDs whose data is available in my database. [1,2,3,3,4,1]

I want to get the data from my list's IDs. I know the solution that I can apply a loop here and get the data by iterating over the list. I was looking for a better solution.

I have seen some similar solution with Point.objects.filter(id__in=[1,2,3,3,4,1]) but it does not return the objects of repeating values like 1 and 3 in my list's case. I want the queryset to have duplicates values if my list has duplicate values. Thanks in advance.

CodePudding user response:

As per your question, understand this point ...

First, you need a unique identifier for getting an object which has similar values. For eg., A group of students has two students whose name is the same. We consider it as "Bob". Now, for identifying the student "Bob" which I have to find, I need his roll no. As we know, roll no. is unique for every student, Likewise your ID list must contain his primary key for identifying which object you want to call.

I hope this answer clears your doubt!

CodePudding user response:

If I read your comment correctly, then you have placed the ForeignKey in the wrong class, since the way you have it now:

class Point(models.Model): 
    p1 = models.IntegerField() 
    p2 = models.IntegerField() 
    
class Graph(models.Model): 
    user=models.ForeignKey(User, on_delete=models.CASCADE) 
    date_created = models.DateTimeField(auto_now_add=True) 
    point = models.ForeignKey(Point, on_delete=models.CASCADE)

There would be MANY graphs to EACH single point. It should be tho other way so that you have MANY points to ONE graph:

class Point(models.Model): 
    p1 = models.IntegerField() 
    p2 = models.IntegerField()
    graph = models.ForeignKey(Graph, on_delete=models.CASCADE)
    
class Graph(models.Model): 
    user=models.ForeignKey(User, on_delete=models.CASCADE) 
    date_created = models.DateTimeField(auto_now_add=True) 

Then, in your view you could get all the Points in your graph like this:

graph = Graph.objects.get(user=..., date_created=...)
# Here is the filter that will give you all Points on the graph:
points = Point.objects.filter(graph=graph)

# If you need the points as an array
points_list = []

for point in points:
    # Your logic here, or if you just need the array you could
    # append to the points_list:
    points_list.append(point)

Please Note
I understand that this still may use a loop, and that the filter in the points = Points.objects.filter(graph=graph) will still not repeat the same point. Again, even though the Point may be the same location, and/or may have other data associated with it, all of which is equal, it is still a unique point with it's own primary key, pk.

If you still need an array (in Python, arrays are called lists) of Points with repeated IDs, then you will have to do a loop, but then the question remains. Do you really need the ID's of the Points? Are you using the ID in your calculations?

  • Related