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`?
On Thu, Dec 19, 2019 at 12:03 PM onlinejudge95 <onlinejudge95@gmail.com> wrote:
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(...).idfor 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
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD%3DM5eSsCtNGh_MFRX2DM6dY1nTmS8vehWWaQHqeH1Jfo1VujQ%40mail.gmail.com.
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKB6Ln-%3DT_JtsGE35giPLJsqX8KwZxWO1fEgF3sJXRatUAmfuA%40mail.gmail.com.
No comments:
Post a Comment