0

I have two functions

def xyz(obj):
   obj_queryset = Sample.objects.filter(id=obj.id)
   callfunction(obj_queryset)

def callfunction(obj_queryset):
   for obj in obj_queryset:
       obj.start_date = datetime.date.today()
       obj.end_date = datetime.date.today()
       obj.save()

I need it as a queryset for certain reasons because i want to update multiple objects and I am also doing few calculations before updating which I am not posting here.

Is there any way to turn obj into a queryset without doing a database query.

Ab1gor
  • 398
  • 3
  • 19
  • Your question is not clear. That *is* a queryset. – Daniel Roseman Oct 22 '18 at 08:05
  • I want to convert _obj_ object into a queryset without doing a database query. Here I am doing a database query using `Sample.objects.filter(id=obj.id)` to turn it into a queryset. I don't want to do a query in the database – Ab1gor Oct 22 '18 at 08:07
  • Firstly, why do you care about a single query; and secondly what are you doing with the queryset in your function? You may or may not be causing a query anyway. – Daniel Roseman Oct 22 '18 at 08:08
  • I need it for certain reasons. Because at certain places I am calling that function using a queryset. As I need to do the operations on multiple objects – Ab1gor Oct 22 '18 at 08:11
  • @PritamRoy: but then you should revise these functions. You can put your object in a list, for example, and as long as the functions only iterate over it, there is no problem. – Willem Van Onsem Oct 22 '18 at 08:12
  • Yeah that is another way. I will have to revise the function if there are no direct methods to convert an object into a queryset.. But the question is whether there is any method other than using Django Filters? – Ab1gor Oct 22 '18 at 08:14
  • The point is that a queryset is just a container that represents a query. That query may or may not have been executed; doing `filter()` itself doesn't itself cause a db hit. Until we see what you are actually trying to do with that queryset, it's impossible to know if you need an extra call or not. – Daniel Roseman Oct 22 '18 at 08:18
  • Ok I am updating the code to reflect what I am doing – Ab1gor Oct 22 '18 at 08:22
  • Well, there is clearly no reason that you need a queryset there; just wrap it in a list as Willem suggested. – Daniel Roseman Oct 22 '18 at 08:32
  • But is there any way to convert object into a queryset? – Ab1gor Oct 22 '18 at 09:15
  • No because it doesn't make sense. A queryset doesn't hold data, it just represents a query to the database. Once the query was executed, you should just use the data. – dirkgroten Oct 22 '18 at 11:05

1 Answers1

0

callfunction does not use anything specific from queryset. It treats its argument as a sequence a.k.a. iterable. Practically speaking it means that anything you can use in a for loop can be passed to the function.

In order to process a single object by this function you can pass a sequence with that object to your function and it can be any sequence that is iterable (not necessarily queryset), for example it can be a list:

callfunction([obj])

This is absolutely valid and it a usual python idiom.