Wednesday, April 22, 2020

Django - ModelChoiceField queryset caching

It would be nice to know a better way to cache the ModelChoiceField's queryset when it's used in a form that runs "is_valid()" in a loop (like Formsets do). The best way I know how at the moment is by not using a ModelChoiceField at all. The solution requires using a ChoiceField, running the query for choices outside of the loop, then passing the choices into the form to override the ChoiceField choices.

Here's an example:

4 comments:

  1. You actually make it look so easy with your performance but I find this matter to be actually something which I think I would never comprehend. It seems too complicated and extremely broad for me. I'm looking forward for your next post, I’ll try to get the hang of it!
    PMP Certification
    PMP Course
    PMP Course in Malaysia
    PMP Training in Malaysia
    PMP Training

    ReplyDelete
  2. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    data analytics course
    big data analytics malaysia
    big data course

    ReplyDelete
  3. I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up..data science course

    ReplyDelete
  4. Thank you for sharing the article. The data that you provided in the blog is informative and effective.

    Tableau Training in Hyderabad

    ReplyDelete