Using generators in Django

23 views
Skip to first unread message

onlinejudge95

unread,
Dec 19, 2019, 1:33:35 AM12/19/19
to django...@googlegroups.com, Python ML
Hi Devs,

A quick question. I am using Django to schedule some commands to populate my PostgreSQL(Apart from using it as a web framework). I am currently fetching records from a particular table and for each of those records doing some calculation and storing the processed data in some other database. The skeleton code looks like this

```
...
def fetch_needs(project_id):
    for item in MyNeedsModel.filter(project_id=project_id).all():
        yield item
...

class Command(django.core.management.base.BaseCommand):
    def add_argument(self, parser):
        ...
    def handler(self, *args, **kwargs):
        project = (args[0], args[1])
        project_id = MyProject.filter(...).id
        for need in fetch_needs(project_id):
            ....
```

I need to know whether the use of generators is correct here, in the sense that would it have any performance issues. The point that I am having trouble understanding is a comment on my code review.

Also don’t use generator you are bypassing django inbuilt caching mechanism. Using integrator it will create another list and get them one by one

Any help would be appreciated.

Thanks,
onlinejudge95 

Mohit Solanki

unread,
Dec 19, 2019, 1:59:40 AM12/19/19
to django...@googlegroups.com
If you only need to access object once while iterating then using iterators is a better choice to reduce memory consumption, Generators won't help here as soon this expression ```MyNeedsModel.filter(project_id=project_id).all()```  is evaluated the all the rows will be fetched from the db at once and will be stored in the queryset object, also about the code review comment, using generators won't bypass django's inbuilt caching mechanism or something, I am not sure about `integrator` maybe he/she meant `iterator`?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD%3DM5eSsCtNGh_MFRX2DM6dY1nTmS8vehWWaQHqeH1Jfo1VujQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages