An optimized way to retrieve ManyToMany fields in a django model in a single request

I have the following model structure in django.

Category:
    - name
    - description

Tag:
    - name

Article:
    - guid
    - title
    - detail
    - tags (Many2Many with Tag)
    - cats (Many2Many with Category)

Now, in my main (up to 100,000) task of updating an article, I have to follow these steps.

- for each article:
     - to check if this article has already appeared in another category
         - if yes, associate article with current category
         - if no, create article along current category
     - to check if any of associated tags are exists or not,
          - if not exists create tag, and associate with current article
          - if exists, just associated with current article

During this operation, I already have a dictionary of existing articles that have been extracted from the database.

Since ManyToMany fields do not affect select operations, such as select_related () in django, is there any other way that can help me load existing articles along with categories and tags?

+3
source share
1 answer

"--", ArticleTag, django ORM select_related :

# Get all 'tag-relations' WITH associated articles AND tags
articles_tags = ArticleTag.objects.all().select_related()

, ... , django , , through -, SQL-. , ram, !

0

Source: https://habr.com/ru/post/1783574/


All Articles