0

I want to use django_filter, but my datatype is list. it shows that list has no attribute model

myfiler1 = Orderfilter(request.GET, queryset=mylist) 

I want to use django_filter, but my datatype is list. it shows that list has no attribute model

myfiler1 = Orderfilter(request.GET, queryset=mylist) 

so how to convert my list to django.db.models.query.QuerySet

user2390182
  • 72,016
  • 6
  • 67
  • 89
hld
  • 31
  • 3
  • Take a look at https://stackoverflow.com/questions/18607698/how-to-convert-a-list-in-to-queryset-django and see if helps. – Stanley Ulili Jun 27 '23 at 06:51
  • You should pass your queryset instead of converting it to a list. The list type and QuerySet are two types. – 911 Jun 27 '23 at 07:01
  • 1
    The best solution is to avoid making a list in the first place. Try real hard to achieve whatever logic you have through the ORM and resist the urge to filter in the more familiar Python. If that is not possible, you can always do the brutish `sq = MyModel.objects.filter(pk__in=[m.pk for m in mylist])` to convert it back to a queryset. – user2390182 Jun 27 '23 at 07:13
  • I have saw this above their data is queryset but my original data type is a list. so is there anyway to convert list to queryset? – hld Jun 27 '23 at 07:23
  • queryset is so difficult to merge or sum by columns, particular hard for dirty data – hld Jun 27 '23 at 07:29
  • sq = MyModel.objects.filter(pk__in=[m.pk for m in mylist]) I saw this, but this is also do something from a queryset, my original data is a list. so what should I do – hld Jun 27 '23 at 07:31
  • It doesn't exactly have to be `[m.pk for m in mylist]`, it depends on the structure of your list. Your goal is to pass a list of primary keys, for example it might be `[m["order_id"] for m in mylist]` – ybl Jun 27 '23 at 07:38
  • @ybl it use the list as a primary keys, so it also from a queryset. but my original original data is a list. – hld Jun 27 '23 at 07:58
  • @hld You can try to filter your original query set first, and then do other complex operations and convert it into a list – 911 Jun 27 '23 at 08:06
  • @hld What I mean is to use `django_filter` to filter your original queryset first, and then do other operations – 911 Jun 27 '23 at 08:20
  • and the final data is different with the orignial querset, because it merge another queryset. so i can't just use the list as a key to select from the original querset – hld Jun 27 '23 at 08:21
  • @hld I have dealt with a situation similar to yours. I merged the query sets of different models, and then filtered their common fields. What I did at the time was to filter them separately using the same method, and then merge them. – 911 Jun 27 '23 at 08:29
  • can you tell me more – hld Jun 27 '23 at 08:42
  • @hld It seems that you want to connect data outside the model to the queryset and filter it, so you can't use the filter in django, because it is based on the model, but these extra data are not in the model, there are two Two methods: 1. Add a new field in the model as the corresponding "count". 2. Manually filter the data list – 911 Jun 29 '23 at 03:11
  • @911 I think your said is right. I want to follow your first advice, and I add a new field to my model. but how can I MERGE this to my model? – hld Jun 29 '23 at 04:55
  • @911我发现你的地址也是北京,谢谢你,我那蹩脚的英文。我就是想把一个字典,利用字典的关键字把字典的值对应到queryset上面。求指导 – hld Jun 29 '23 at 05:29
  • @hld 尴尬,我还一直用翻译来着,如果你这个字典不会一直变的话,你写个脚本就可以了,把字典里的值同步到模型上,如果他是动态的话,你只能自己实现筛选了 – 911 Jun 29 '23 at 05:39
  • @911@ user2390182 thank you. I maded it finally by using .objects.filter and .objects.bulk_update – hld Jun 29 '23 at 08:30

1 Answers1

0

You need to pass orderdata = myfiler1.qs

orderdata = OrdersModel.objects.filter(user=request.user)
myfiler1 = Orderfilter(request.GET, queryset=orderdata) 
orderdata = myfiler1.qs