sourcenet-to-context-dev

This notebook contains the development and testing code for moving data from the sourcenet project, in context_text, to the context core.

Moved dev work for exporting network data from context to ./render-context-networks-dev.ipynb.

Table of Contents

notes and questions

Notes:

  • probably will need a class that is an Article_Data container - reference to Article_Data, and then all the logic to process the entities and relations contained within.

Questions:

  • Do we want an Identifier Type separate from Entity and Relation identifiers? I think we do, so we can specify the entity type(s) a given identifier should be used on.

    • Created abstract one, and created a concrete entity identifier type. Will create one for relation identifiers if needed.

Setup


In [1]:
me = "sourcenet-to-context-dev"

Setup - Debug


In [2]:
debug_flag = False

Setup - Imports


In [3]:
import datetime
from django.db.models import Avg, Max, Min, Q
from django.utils.text import slugify
import json
import logging
import six

Setup - working folder paths


In [4]:
%pwd


Out[4]:
'/home/jonathanmorgan/work/django/research/work/phd_work/analysis'

In [5]:
# current working folder
current_working_folder = "/home/jonathanmorgan/work/django/research/work/phd_work/analysis"
current_datetime = datetime.datetime.now()
current_date_string = current_datetime.strftime( "%Y-%m-%d-%H-%M-%S" )

Setup - logging

configure logging for this notebook's kernel (If you do not run this cell, you'll get the django application's logging configuration.


In [6]:
logging_file_name = "{}/logs/{}-{}.log.txt".format( current_working_folder, me, current_date_string )
logging.basicConfig(
    level = logging.DEBUG,
    format = '%(asctime)s - %(levelname)s - %(name)s - %(message)s',
    filename = logging_file_name,
    filemode = 'w' # set to 'a' if you want to append, rather than overwrite each time.
)
print( "Logging initialized, to {}".format( logging_file_name ) )


Logging initialized, to /home/jonathanmorgan/work/django/research/work/phd_work/analysis/sourcenet-to-context-dev-2019-12-21-05-44-17.log.txt

Setup - virtualenv jupyter kernel

If you are using a virtualenv, make sure that you:

  • have installed your virtualenv as a kernel.
  • choose the kernel for your virtualenv as the kernel for your notebook (Kernel --> Change kernel).

Since I use a virtualenv, need to get that activated somehow inside this notebook. One option is to run ../dev/wsgi.py in this notebook, to configure the python environment manually as if you had activated the sourcenet virtualenv. To do this, you'd make a code cell that contains:

%run ../dev/wsgi.py

This is sketchy, however, because of the changes it makes to your Python environment within the context of whatever your current kernel is. I'd worry about collisions with the actual Python 3 kernel. Better, one can install their virtualenv as a separate kernel. Steps:

  • activate your virtualenv:

      workon research
  • in your virtualenv, install the package ipykernel.

      pip install ipykernel
  • use the ipykernel python program to install the current environment as a kernel:

      python -m ipykernel install --user --name <env_name> --display-name "<display_name>"
    
    

    sourcenet example:

      python -m ipykernel install --user --name sourcenet --display-name "research (Python 3)"

More details: http://ipython.readthedocs.io/en/stable/install/kernel_install.html

Setup - Initialize Django

First, initialize my dev django project, so I can run code in this notebook that references my django models and can talk to the database using my project's settings.


In [7]:
# init django
django_init_folder = "/home/jonathanmorgan/work/django/research/work/phd_work"
django_init_path = "django_init.py"
if( ( django_init_folder is not None ) and ( django_init_folder != "" ) ):
    
    # add folder to front of path.
    django_init_path = "{}/{}".format( django_init_folder, django_init_path )
    
#-- END check to see if django_init folder. --#

In [8]:
%run $django_init_path


django initialized at 2019-12-21 05:44:22.840687

In [9]:
# context imports
from context.export.network.filter_spec import FilterSpec
from context.export.network.network_data_request import NetworkDataRequest
from context.models import Entity
from context.models import Entity_Identifier_Type
from context.models import Entity_Identifier
from context.models import Entity_Relation
from context.models import Entity_Type
from context.tests.export.network.test_helper import TestHelper

# context_text imports
from context_text.article_coding.article_coding import ArticleCoder
from context_text.article_coding.article_coding import ArticleCoding
from context_text.article_coding.open_calais_v2.open_calais_v2_article_coder import OpenCalaisV2ArticleCoder
from context_text.collectors.newsbank.newspapers.GRPB import GRPB
from context_text.collectors.newsbank.newspapers.DTNB import DTNB
from context_text.export.to_context_base.export_to_context import ExportToContext
from context_text.models import Article
from context_text.models import Article_Subject
from context_text.models import Newspaper
from context_text.shared.context_text_base import ContextTextBase

Setup - ExportToContext instance

Make instance, set instance variables.


In [10]:
my_exporter = ExportToContext()

# no variables to set, yet...
my_exporter.set_article_uuid_id_type_name( ExportToContext.ENTITY_ID_TYPE_ARTICLE_NEWSBANK_ID )


Out[10]:
<Entity_Identifier_Type: 4 - article_newsbank_id - Newsbank - ( article )>

Setup - Initialize LoggingHelper

Create a LoggingHelper instance to use to log debug and also print at the same time.

Preconditions: Must be run after Django is initialized, since python_utilities is in the django path.


In [11]:
# python_utilities
from python_utilities.logging.logging_helper import LoggingHelper

# init
my_logging_helper = LoggingHelper()
my_logging_helper.set_logger_name( me )
log_message = None

set up context entity and relation types

Set up the following entity and relation types in context:

Entity_Identifier_Types:

  • general:

    • permalink: name permalink (fixture ID 5) - CONTEXT_ENTITY_ID_TYPE_PERMALINK
  • article:

    • article archive identifier: name article_archive_identifier (fixture ID 6) - CONTEXT_ENTITY_ID_TYPE_ARTICLE_ARCHIVE_IDENTIFIER
    • sourcenet article ID: name article_sourcenet_id (fixture ID 3) - CONTEXT_ENTITY_ID_TYPE_ARTICLE_SOURCENET_ID
    • newsbank article ID: name article_newsbank_id (fixture ID 4) - CONTEXT_ENTITY_ID_TYPE_ARTICLE_NEWSBANK_ID
  • newspaper:

    • sourcenet newspaper ID: name newspaper_sourcenet_id (fixture ID 7) - CONTEXT_ENTITY_ID_TYPE_NEWSPAPER_SOURCENET_ID
    • newsbank newspaper code: name newspaper_newsbank_code (fixture ID 8) - CONTEXT_ENTITY_ID_TYPE_NEWSPAPER_NEWSBANK_CODE
  • organization:

    • sourcenet organization ID: name organization_sourcenet_id (fixture ID 9) - CONTEXT_ENTITY_ID_TYPE_ORGANIZATION_SOURCENET_ID
  • person:

    • sourcenet person ID: name person_sourcenet_id (fixture ID 1) - CONTEXT_ENTITY_ID_TYPE_NAME_PERSON_SOURCENET_ID
    • OpenCalais URI for person: name person_open_calais_uuid (fixture ID 2) - CONTEXT_ENTITY_ID_TYPE_NAME_PERSON_OPEN_CALAIS_UUID

Entities:

  • person - a person!
  • article - Article, used to detect reporters, subjects, and sources.
  • organization - Any type of organized group (of organizations, of people, etc.).
  • newspaper - A newspaper, which can be used to cluster articles, employees, sources, etc.

Relations:

  • from newspaper

    • newspaper_reporter - Reporter at a newspaper, evidence of which is byline on an article in that newspaper. FROM newspaper TO person (reporter) THROUGH article.
    • newspaper_source - Person quoted in an article published by a newspaper. FROM newspaper TO person (source) THROUGH article.
    • newspaper_subject - Subject of an article published in a given newspaper. FROM newspaper TO person (subject, including sources) THROUGH article.
    • newspaper_article - Article published in a particular newspaper. FROM newspaper TO article.
  • from article

    • author - Author/Reporter of an article - FROM article TO reporter.
    • subject - Subject of a story. FROM article TO subject person.
    • source - Source quoted in an article - FROM article TO source person.
  • through article

    • article_container - Parent for relations based on entities being mentioned in the same article. To start, just people, but eventually, for example, could also include location.
    • mentioned - Mentioned in an article. FROM reporter/author TO subject THROUGH article.
    • quoted - The \"from\" person quoted the \"to\" person in a publication. FROM reporter TO source THROUGH article.
    • same_article_sources - Sources in the same article, FROM source person TO source person THROUGH article.
    • same_article_subjects - Two people who are in a particular article together (includes subjects and sources).
    • shared_byline - Shared Byline on an article - joint authors - FROM author TO author THROUGH article.

Export entity and relation types to JSON fixtures

Then, export them to JSON fixture files using manage.py / django-admin dumpdata ( https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-dumpdata ) so they can be imported using python manage.py or django-admin loaddata ( https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-loaddata ) rather than having to input them in the admin:

python manage.py dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]] --indent INDENT --output <output_file_path>

python manage.py dumpdata \
    --indent 4 \
    --output context-sourcenet_entity_and_relation_types.json \
    context.Entity_Identifier_Type \
    context.Entity_Relation_Type \
    context.Entity_Relation_Type_Trait \
    context.Entity_Type \
    context.Entity_Type_Trait \
    context.Trait_Type \
    context.Term \
    context.Term_Relation \
    context.Term_Relation_Type \
    context.Vocabulary \

No line breaks:

    python manage.py dumpdata --indent 4 --output context-sourcenet_entity_and_relation_types.json context.Entity_Identifier_Type context.Entity_Relation_Type context.Entity_Relation_Type_Trait context.Entity_Type context.Entity_Type_Trait context.Trait_Type context.Term context.Term_Relation context.Term_Relation_Type context.Vocabulary

Load to database from fixture file

Seed entities and relations for sourcenet from fixture file created above, stored as a fixture in context.

  • store the JSON from the context fixture in Context_Text_Base?
  • make a simple class method there that loops over the items in the JSON and looks each up. If it finds it, updates it. If not, creates it.

Example JSON:

{
    "model": "context.entity_identifier_type",
    "pk": 8,
    "fields": {
        "create_date": "2019-11-01T15:27:29.526Z",
        "last_modified": "2019-11-01T15:27:29.526Z",
        "name": "newspaper_newsbank_code",
        "label": "newspaper_newsbank_code",
        "source": "Newsbank",
        "notes": "3-letter code assigned to a given paper by NewsBank.",
        "type_list": [
            7
        ]
    }
},

In [ ]:
# declare variables
path_to_context_fixture = None
json_file = None
fixture_json = None

# read in the fixture file.
path_to_context_fixture = "/home/jonathanmorgan/work/django/research/context/fixtures/context-sourcenet_entities_and_relations.json"

# load the fixture JSON into memory.
with open( path_to_context_fixture ) as json_file:  

    # parse the JSON file.
    fixture_json = json.load( json_file )
    
#-- END open of JSON file to read it into memory. --#

In [ ]:
print( json.dumps( fixture_json, sort_keys = True, indent = 4 ) )

In [ ]:
# declare variables
fixture_item = None
item_model = None
item_fields = None

# declare variables - Entity_Identifier_Type
id_type_model = None
id_type_qs = None
id_type_count = None
id_type_instance = None
id_type_name = None
id_type_label = None
id_type_source = None
id_type_notes = None
id_type_type_list = None

# declare variables - associated Entity_Type s
entity_type_id = None
entity_type_qs = None
entity_type_count = None
entity_type_instance = None

# init
id_type_model = "context.entity_identifier_type"

# create or update Entity_Idetifier_Types

# loop over items in fixture JSON
for fixture_item in fixture_json:
    
    # get model, so we can see what we do.
    item_model = fixture_item.get( "model", None )
    
    # is it "context.entity_identifier_type"?
    if ( item_model == id_type_model ):
        
        # it is an identifier type.  Retrieve fields.
        item_fields = fixture_item.get( "fields", None )
        id_type_name = item_fields.get( "name", None )
        
        # do we have a name?  (Better have one...)
        if ( id_type_name is not None ):
            
            print( "\nEntity_Identifier_Type: {}".format( id_type_name ) )
        
            # does it already exist?
            id_type_qs = Entity_Identifier_Type.objects.all()
            id_type_qs = id_type_qs.filter( name__iexact = id_type_name )
            id_type_count = id_type_qs.count()
            
            # how many matches
            if ( id_type_count == 0 ):
                
                # it does not exist.  Create.
                
                # retrieve values
                id_type_label = item_fields.get( "label", None )
                id_type_source = item_fields.get( "source", None )
                id_type_notes = item_fields.get( "notes", None )
                id_type_type_list = item_fields.get( "type_list", [] )
                
                # store values and save.
                id_type_instance = Entity_Identifier_Type()
                id_type_instance.name = id_type_name
                id_type_instance.label = id_type_label
                id_type_instance.source = id_type_source
                id_type_instance.notes = id_type_notes
                id_type_instance.save()
                
                # add related entity types.
                for entity_type_id in id_type_type_list:
                    
                    # look up the Entity_Type
                    entity_type_qs = Entity_Type.objects.all()
                    entity_type_qs = entity_type_qs.filter( pk = entity_type_id )
                    entity_type_count = entity_type_qs.count()
                    
                    # how many matches?
                    if ( entity_type_count == 1 ):
                        
                        # one.  Associate it.
                        entity_type_instance = entity_type_qs.get()
                        id_type_instance.type_list.add( entity_type_instance )
                        
                    elif ( entity_type_count == 0 ):
                        
                        # no match - move on.
                        print( "no matching Entity_Type for id {}, moving on.".format( entity_type_id ) )
                        
                    elif ( entity_type_count > 1 ):
                        
                        # multiple pk matches - ERROR.
                        print( "ERROR - multiple matching Entity_Type for id {}, moving on.".format( entity_type_id ) )
                        
                    else:

                        # multiple pk matches - ERROR.
                        print( "ERROR - multiple matching Entity_Type for id {}, moving on.".format( entity_type_id ) )
                    
                    #-- END check to see if entity type found --#
                    
                #-- END loop over related entity types. --#
                
                print( "----> ADDED Entity_Identifier_Type for name {}.\n==> JSON:\n{}\n==> instance:\n{}".format( id_type_name, fixture_item, id_type_instance ) )

            elif ( id_type_count == 1 ):
                
                # exists.  Update?
                id_type_instance = id_type_qs.get()
                print( "Entity_Identifier_Type for name {} already exists.\n==> JSON:\n{}\n==> instance:\n{}".format( id_type_name, fixture_item, id_type_instance ) )
            
            elif ( id_type_count > 1 ):
                
                # error.
                print( "ERROR - more than one type match ({}) for name {}".format( id_type_count, id_type_name ) )
                
            else:
                
                # unexpected error.
                print( "ERROR - name {} does not have 0, 1, or > 1 matches (count = {}).".format( id_type_name, id_type_count ) )
                
            #-- END check to see how many matches --#
            
        else:
            
            # ERROR
            print( "ERROR - no name for Entity_Identifier_Type." )
            
        #-- END check to see if name. --#
        
    #-- END check to see if Entity_Identifier_Type --#
    
#-- END loop over fixture items. --#

load sourcenet data into context

logic:

  • get all Article_Data created by automated coder, open calais coder type.
  • for each:

    • Article processing:

      • look for article entity in context. If not created, create one of type "article". Store the sourcenet ID as identifier of type "article_sourcenet_id".
      • Store the ID and entity reference for the Article (in object? Should I make a place for the article to hold an Entity ID?).
      • check to see if the newspaper has an entity (using its sourcenet ID as the lookup identifier). If not, create one.
    • Person processing:

      • Add all people who are either reporters/authors or subjects to context as entities of type "person", with identifier of type person_sourcenet_id set to their internal django/database "id", and with identifier of type person_open_calais_uuid set to their OpenCalais ID, if they have one. If they have any other types of IDs, add them too, untyped. Once entity is created, store ForeignKey of entity in Person record.
      • Author/Reporter processing:

        • look for each author's person entity. If not found, create one.
      • Subject/Source processing:

        • look for a person entity for each subject/source. If not found, create one.
    • Relations - create the following:

      • from newspaper

        • newspaper_reporter - Reporter at a newspaper, evidence of which is byline on an article in that newspaper. FROM newspaper TO person (reporter) THROUGH article.
        • newspaper_source - Person quoted in an article published by a newspaper. FROM newspaper TO person (source) THROUGH article.
        • newspaper_subject - Subject of an article published in a given newspaper. FROM newspaper TO person (subject, including sources) THROUGH article.
        • newspaper_article - Article published in a particular newspaper. FROM newspaper TO article.
      • from article

        • author - Author/Reporter of an article - FROM article TO reporter.
        • subject - Subject of a story. FROM article TO subject person.
        • source - Source quoted in an article - FROM article TO source person.
      • through article

        • article_container - Parent for relations based on entities being mentioned in the same article. To start, just people, but eventually, for example, could also include location.
        • mentioned - Mentioned in an article. FROM reporter/author TO subject THROUGH article.
        • quoted - The "from" person quoted the "to" person in a publication. FROM reporter TO source THROUGH article.
        • same_article_sources - Sources in the same article, FROM source person TO source person THROUGH article.
        • same_article_subjects - Two people who are in a particular article together (includes subjects and sources).
        • shared_byline - Shared Byline on an article - joint authors - FROM author TO author THROUGH article.

Relations broken out by person type:

  • For all reporters/authors:

    • create a relation of type "author" between the article's entity (FROM) and the entity of the person (TO) for each author.
    • if multiple authors, for each pair of authors, create a relation of type "shared_byline" between the two (it is undirected), THROUGH the article.
  • For all subjects, including sources:

    • create a relation of type "subject" between the article's entity (FROM) and the entity of the subject person (TO).
    • if multiple subjects, for each pair of subjects, create a relation of type "same_article_subjects" between the two (it is undirected), THROUGH the article.
    • create a relation of type "mentioned" between each of the article's authors (FROM) and the subject (TO), THROUGH the article.
  • For all sources

    • create a relation of type "source" between the article's entity and the entity for the source person.
    • if multiple sources, for each pair of sources, create a relation of type "same_article_sources" between the two (it is undirected), THROUGH the article.
    • create a relation of type "quoted" between each of the article's authors (FROM) and the source (TO), THROUGH the article.

Convenience methods:

  • method to find entity - based on type and identifier (accept all the fields that make sense, including optional identifier type instance).
  • method to find relation - based on type, etc.

Notes:

  • all ties are undirected.
  • relations can have three foreign keys into Entity - FROM, TO, and THROUGH (for a containing relationship, like the article that included two sources that we are relating).

Retrieve Article instances

Before we do anything else, need to be able to pull back all the articles whose data we want to load into the context store.


In [12]:
# look for publications that have article data:
# - coded by automated coder
# - with coder type of "OpenCalais_REST_API_v2"

# get automated coder
automated_coder_user = ContextTextBase.get_automated_coding_user()

log_message = "{} - Loaded automated user: {}, id = {}".format( datetime.datetime.now(), automated_coder_user, automated_coder_user.id )
my_logging_helper.output_message( log_message, do_print_IN = True, log_level_code_IN = logging.INFO )


2019-11-26 02:55:44.555990 - Loaded automated user: automated, id = 7

In [13]:
# find articles with Article_Data created by the automated user...
article_qs = Article.objects.filter( article_data__coder = automated_coder_user )

# ...and specifically coded using OpenCalais V2...
article_qs = article_qs.filter( article_data__coder_type = OpenCalaisV2ArticleCoder.CONFIG_APPLICATION )

# ...and finally, we just want the distinct articles by ID.
article_qs = article_qs.order_by( "id" ).distinct( "id" )

# count?
article_count = article_qs.count()
log_message = "Found {} articles".format( article_count )
my_logging_helper.output_message( log_message, do_print_IN = True, log_level_code_IN = logging.INFO )


Found 46 articles

build load code and unit tests

Testing

First, make unit tests to test convenience methods added to the following models, in context/tests/models/:

  • // Entity_Identifier_Type - test_Entity_Identifier_Type_model.py
  • // Entity_Identifier - test_Entity_Identifier_model.py
  • // Entity - test_Entity_model.py

Also, // move instance creation class methods along with their constants over into "TestHelper" from test_Entity_Identifier_model.py, so they can be re-used across test classes.

To run: python manage.py test context.tests

In test data:

  • article 21925:

    • Article_Data: SELECT * FROM context_text_article_data WHERE article_id = 21925;

TODO:

  • test_entity_model.py

    • test_get_entity_for_identifier
  • test_export_to_context.py

    • test_get_article_uuid_id_type
    • test_set_article_uuid_id_type_name

Load data from Article instances

Now, we actually load the data...


In [14]:
result = my_exporter.process_articles( article_qs )


In create_q_filter_automated_by_coder_type: automated coder user: 7 - automated

Summary:
- good count: 46
- > 1 count: 0
- 0 count: 0
- unexpected count: 0

Filter in context

Now, we need to filter data in context, so we can test to make sure the data we expect was added.

Specify filter criteria

First step is to translate the filter criteria for nodes and ties from the existing admin for the querying context.

Old filter criteria

Configuration of Network Builder, from methods-network_analysis-create_network_data.ipynb:

  • Configuration to generate network files for prelim:

    • Config of "Select Articles" - fields in bold need to be changed from default values:

      • Start date (YYYY-MM-DD): 2009-12-01
      • End date (YYYY-MM-DD): 2009-12-31
      • Fancy date range: - Empty.
      • Publications: "Grand Rapids Press, The"
      • Coders: None selected.
      • Coder IDs to include, in order of highest to lowest priority:

        • for human sample: 13,8,9,10
        • for automated: 2
      • if automated: Article_Data coder_type Filter Type and coder_type 'Value In' List (comma-delimited):

        • only for coder "automated" (2), for now.
        • use the coder_type filter fields to filter automatically coded Article_Data on coder type if you have tried different automated coder types:

          • Article_Data coder_type Filter Type: - Just automated
          • coder_type 'Value In' List (comma-delimited): - Enter the coder types you want included. Examples:

            • OpenCalais v2: "OpenCalais_REST_API_v2"
      • Topics: None selected.

      • Article Tag List (comma-delimited): - "grp_month"
      • Unique Identifier List (comma-delimited): - Empty.
      • Allow duplicate articles: - "No"
    • Configure "Network Settings" - fields in bold need to be changed from default values:

      • relations - Include source contact types - All selected.
      • relations - Include source capacities: - None selected.
      • relations - Exclude source capacities: - None selected.
      • Download as File? - "Yes"
      • Include render details? - "No"
      • Data Format: - "Tab-Delimited Matrix"
      • Data Output Type: - "Network + Attribute Columns"
      • Network Label: - Empty.
      • Include Headers: - "Yes"
    • Config of "Select People" - fields in bold need to be changed from default values:

      • NOTE: This will be the same for all networks you want to compare (different weeks within a month, compared to the whole month, for instance). For each, get people from articles that are filtered to include all people used by either human or automated, and all the days covered by any of the networks you want to compare. This means you'll have the same dimensions of network (same set of nodes/people) regardless of the particular network you are generating, allowing the matrices that result to be compared.
      • Person Query Type: - "Custom, defined below"
      • People from (YYYY-MM-DD): - 2009-12-01
      • People to (YYYY-MM-DD): - 2009-12-31
      • Fancy person date range: - Empty.
      • Person publications: - "Grand Rapids Press, The"
      • Person coders: - "automated", "minnesota1", "minnesota2", "minnesota3", "ground_truth"
      • Coder IDs to include, in order of highest to lowest priority: - Empty.
      • Article_Data coder_type Filter Type and coder_type 'Value In' List (comma-delimited):

        • NOTE: not just for automated - since this includes all coders, automated and human, you need to always specify the coder type filter if you need it for automated network.
        • use the coder_type filter fields to filter automatically coded Article_Data on coder type if you have tried different automated coder types:

          • Article_Data coder_type Filter Type: - Just automated
          • coder_type 'Value In' List (comma-delimited): - Enter the coder types you want included. Examples:

            • OpenCalais v2: "OpenCalais_REST_API_v2"
      • Person Topics: None

      • Article Tag List (comma-delimited): - "grp_month"
      • Unique Identifier List (comma-delimited): - Empty.
      • Person allow duplicate articles: - "Yes"

JSON files of filter criteria for just automated coding

Below is a JSON file that is just the automated coding for the month from 2009-12-01 through 2009-12-31. Just about all of the complexity of the original screens is possible here, as long as you loaded the entities and ties and all of the needed traits, including some way of adding tags...

{
    "output_specification": {
        "output_type": "file",
        "output_file_path": "./NetworkDataRequest_test_output.txt",
        "output_format": "TSV_matrix",
        "output_structure": "both_trait_columns",
        "output_include_column_headers": true
    },
    "relation_selection": {
        "relation_type_slug_filter_combine_type": "AND",
        "relation_type_slug_filters": [
            {
                "comparison_type": "includes",
                "value_list": [ "mentioned", "qouted", "shared_byline" ]
            }
        ],
        "relation_trait_filter_combine_type": "AND",
        "relation_trait_filters": [
            {
                "name": "pub_date",
                "data_type": "date",
                "comparison_type": "in_range",
                "value_from": "2009-12-01",
                "value_to": "2009-12-31"
            },
            {
                "name": "sourcenet-coder-User-username",
                "data_type": "string",
                "comparison_type": "includes",
                "value_list": [ "automated" ]
            },
            {
                "name": "coder_type",
                "data_type": "string",
                "comparison_type": "includes",
                "value_list": [ "OpenCalais_REST_API_v2" ]
            }
        ],
        "entity_type_slug_filter_combine_type": "AND",
        "entity_type_slug_filters": [
            {
                "comparison_type": "includes",
                "value_list": [ "person" ],
                "relation_roles_list": [ "FROM" ]
            },
            {
                "comparison_type": "includes",
                "value_list": [ "person" ],
                "relation_roles_list": [ "TO" ]
            },
            {
                "comparison_type": "includes",
                "value_list": [ "article" ],
                "relation_roles_list": [ "THROUGH" ]
            }            
        ],
        "entity_trait_filter_combine_type": "AND",
        "entity_trait_filters": [
            {
                "name": "sourcenet-Newspaper-ID",
                "data_type": "int",
                "comparison_type": "includes",
                "value_list": [ 1 ],
                "relation_roles_list": [ "THROUGH" ]
            }
        ]
    }
}

Create test entity and relation data

To start, try making a fixture of all of the context app from the temp database. This should be entities and ties for the 50 or so articles we coded there.

Including entity and relation types

This includes contents of file "context-sourcenet_entity_and_relation_types.json", so you can just load this one file.

Export to JSON fixture files using manage.py / django-admin dumpdata ( https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-dumpdata ) so they can be imported using python manage.py or django-admin loaddata ( https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-loaddata ) rather than having to input them in the admin:

python manage.py dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]] --indent INDENT --output <output_file_path>

python manage.py dumpdata \
    --indent 4 \
    --output context-sourcenet_entities_and_relations-full.json \
    context

No line breaks:

    python manage.py dumpdata --indent 4 --output context-sourcenet_entities_and_relations-full.json context

Excluding entity and relation types

This excludes contents of file "context-sourcenet_entity_and_relation_types.json", so load that first, then this.

Export to JSON fixture files using manage.py / django-admin dumpdata ( https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-dumpdata ) so they can be imported using python manage.py or django-admin loaddata ( https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-loaddata ) rather than having to input them in the admin:

python manage.py dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]] --indent INDENT --output <output_file_path>

python manage.py dumpdata \
    --indent 4 \
    --output context-sourcenet_entities_and_relations.json \
    --exclude context.Entity_Identifier_Type \
    --exclude context.Entity_Relation_Type \
    --exclude context.Entity_Relation_Type_Trait \
    --exclude context.Entity_Type \
    --exclude context.Entity_Type_Trait \
    --exclude context.Trait_Type \
    --exclude context.Term \
    --exclude context.Term_Relation \
    --exclude context.Term_Relation_Type \
    --exclude context.Vocabulary \
    context

No line breaks:

    python manage.py dumpdata --indent 4 --output context-sourcenet_entities_and_relations.json --exclude context.Entity_Identifier_Type --exclude context.Entity_Relation_Type --exclude context.Entity_Relation_Type_Trait --exclude context.Entity_Type --exclude context.Entity_Type_Trait --exclude context.Trait_Type --exclude context.Term --exclude context.Term_Relation --exclude context.Term_Relation_Type --exclude context.Vocabulary context

Test filter code

For each type of filter, need to prototype the code to implement the filter.

filter on entity identifiers

Figure out which relation roles we are focusing on (combination of FROM, TO, THROUGH), then build out query to properly filter.

filter just entities


In [26]:
# try to just find the entity with the desired identifier.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
entity_qs = entity_qs.filter( entity_identifier__name = "person_sourcenet_id" )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
entity_qs = entity_qs.filter( entity_identifier__uuid = "202" )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 265 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") WHERE "context_entity_identifier"."name" = person_sourcenet_id)

result count: 1 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") INNER JOIN "context_entity_identifier" T3 ON ("context_entity"."id" = T3."entity_id") WHERE ("context_entity_identifier"."name" = person_sourcenet_id AND T3."uuid" = 202))

In [27]:
# try to just find the entity with the desired identifier.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
combined_q = Q( entity_identifier__name = "person_sourcenet_id" ) & Q( entity_identifier__uuid = "202" )
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 1 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") WHERE ("context_entity_identifier"."name" = person_sourcenet_id AND "context_entity_identifier"."uuid" = 202))

filter relations


In [28]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# try to find relations to an Entity in any relation role (FROM, TO, THROUGH), with identifier name "`person_sourcenet_id`" and id 202.
from_q = Q( relation_from__entity_identifier__name = "person_sourcenet_id" ) & Q( relation_from__entity_identifier__uuid = "202" )
relation_qs = relation_qs.filter( from_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 11 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity" ON ("context_entity_relation"."relation_from_id" = "context_entity"."id") INNER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") WHERE ("context_entity_identifier"."name" = person_sourcenet_id AND "context_entity_identifier"."uuid" = 202) )

In [29]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# try to find relations to an Entity in any relation role (FROM, TO, THROUGH), with identifier name "`person_sourcenet_id`" and id 202.
to_q = Q( relation_to__entity_identifier__name = "person_sourcenet_id" ) & Q( relation_to__entity_identifier__uuid = "202" )
relation_qs = relation_qs.filter( to_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 17 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity" ON ("context_entity_relation"."relation_to_id" = "context_entity"."id") INNER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") WHERE ("context_entity_identifier"."name" = person_sourcenet_id AND "context_entity_identifier"."uuid" = 202) )

In [30]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# try to find relations to an Entity in any relation role (FROM, TO, THROUGH), with identifier name "`person_sourcenet_id`" and id 202.
through_q = Q( relation_through__entity_identifier__name = "person_sourcenet_id" ) & Q( relation_through__entity_identifier__uuid = "202" )
relation_qs = relation_qs.filter( through_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity" ON ("context_entity_relation"."relation_through_id" = "context_entity"."id") INNER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") WHERE ("context_entity_identifier"."name" = person_sourcenet_id AND "context_entity_identifier"."uuid" = 202) )

In [31]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# try to find relations to an Entity in any relation role (FROM, TO, THROUGH), with identifier name "`person_sourcenet_id`" and id 202.
from_q = Q( relation_from__entity_identifier__name = "person_sourcenet_id" ) & Q( relation_from__entity_identifier__uuid = "202" )
to_q = Q( relation_to__entity_identifier__name = "person_sourcenet_id" ) & Q( relation_to__entity_identifier__uuid = "202" )
through_q = Q( relation_through__entity_identifier__name = "person_sourcenet_id" ) & Q( relation_through__entity_identifier__uuid = "202" )
combined_q = from_q | to_q | through_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 216 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity" ON ("context_entity_relation"."relation_from_id" = "context_entity"."id") LEFT OUTER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") INNER JOIN "context_entity" T4 ON ("context_entity_relation"."relation_to_id" = T4."id") LEFT OUTER JOIN "context_entity_identifier" T5 ON (T4."id" = T5."entity_id") LEFT OUTER JOIN "context_entity" T6 ON ("context_entity_relation"."relation_through_id" = T6."id") LEFT OUTER JOIN "context_entity_identifier" T7 ON (T6."id" = T7."entity_id") WHERE (("context_entity_identifier"."name" = person_sourcenet_id AND "context_entity_identifier"."uuid" = 202) OR (T5."name" = person_sourcenet_id AND T5."uuid" = 202) OR (T7."name" = person_sourcenet_id AND T7."uuid" = 202)) )

In [34]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

entity_qs = Entity.objects.filter( pk = 315 )

# try to find relations to an Entity in any relation role (FROM, TO, THROUGH), with identifier name "`person_sourcenet_id`" and id 202.
from_q = Q( relation_from__in = entity_qs )
combined_q = from_q
test_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

to_q = Q( relation_to__in = entity_qs )
combined_q = combined_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

through_q = Q( relation_through__in = entity_qs )
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 11 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" = 315) )

result count: 28 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" = 315) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" = 315)) )

result count: 28 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" = 315) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" = 315) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" = 315)) )

test IN list of sourcenet_person_id


In [15]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "46" )
test_value_list.append( "163" )
test_value_list.append( "161" )
test_value_list.append( "164" )
test_value_list.append( "30" )
test_value_list.append( "175" )

combined_q = Q( entity_identifier__name = "person_sourcenet_id" ) & Q( entity_identifier__uuid__in = test_value_list )
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {}".format( entity_id ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 6 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") WHERE ("context_entity_identifier"."name" = person_sourcenet_id AND "context_entity_identifier"."uuid" IN (46, 163, 161, 164, 30, 175)))
Entity ID: 8
Entity ID: 9
Entity ID: 11
Entity ID: 16
Entity ID: 18
Entity ID: 20

In [16]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 88 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 9, 11, 16, 18, 20)) )

TO - result count: 63 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 9, 11, 16, 18, 20)) )

THROUGH - result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 9, 11, 16, 18, 20)) )

FROM | TO - result count: 135 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 9, 11, 16, 18, 20)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 9, 11, 16, 18, 20))) )

FROM | TO | THROUGH - result count: 135 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 9, 11, 16, 18, 20)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 9, 11, 16, 18, 20)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 9, 11, 16, 18, 20))) )

test NOT IN list of sourcenet_person_id


In [24]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "20202020" )
test_value_list.append( "20202021" )
test_value_list.append( "20202022" )

id_name_q = Q( entity_identifier__name = "person_sourcenet_id" )
id_value_q = Q( entity_identifier__uuid__in = test_value_list )
combined_q = id_name_q & ( ~ id_value_q )

#entity_qs = entity_qs.filter( id_name_q )
#entity_qs = entity_qs.exclude( id_value_q )
entity_qs = entity_qs.filter( combined_q )

print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 265 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") WHERE ("context_entity_identifier"."name" = person_sourcenet_id AND NOT ("context_entity"."id" IN (SELECT U1."entity_id" FROM "context_entity_identifier" U1 WHERE (U1."uuid" IN (20202020, 20202021, 20202022) AND U1."id" = ("context_entity_identifier"."id"))))))
Entity ID: 8 ( context_text-Person-161 )
Entity ID: 28 ( context_text-Person-182 )
Entity ID: 30 ( context_text-Person-180 )
Entity ID: 13 ( context_text-Person-166 )
Entity ID: 14 ( context_text-Person-186 )
Entity ID: 15 ( context_text-Person-162 )
Entity ID: 16 ( context_text-Person-163 )
Entity ID: 27 ( context_text-Person-877 )
Entity ID: 10 ( context_text-Person-872 )
Entity ID: 12 ( context_text-Person-165 )
Entity ID: 18 ( context_text-Person-30 )
Entity ID: 20 ( context_text-Person-175 )
Entity ID: 22 ( context_text-Person-176 )
Entity ID: 23 ( context_text-Person-173 )
Entity ID: 24 ( context_text-Person-172 )
Entity ID: 25 ( context_text-Person-32 )
Entity ID: 19 ( context_text-Person-750 )
Entity ID: 21 ( context_text-Person-174 )
Entity ID: 31 ( context_text-Person-876 )
Entity ID: 32 ( context_text-Person-181 )
Entity ID: 33 ( context_text-Person-875 )
Entity ID: 29 ( context_text-Person-878 )
Entity ID: 9 ( context_text-Person-46 )
Entity ID: 11 ( context_text-Person-164 )
Entity ID: 63 ( context_text-Person-36 )
Entity ID: 64 ( context_text-Person-272 )
Entity ID: 36 ( context_text-Person-179 )
Entity ID: 39 ( context_text-Person-914 )
Entity ID: 41 ( context_text-Person-909 )
Entity ID: 42 ( context_text-Person-213 )
Entity ID: 43 ( context_text-Person-910 )
Entity ID: 44 ( context_text-Person-132 )
Entity ID: 45 ( context_text-Person-912 )
Entity ID: 46 ( context_text-Person-215 )
Entity ID: 47 ( context_text-Person-911 )
Entity ID: 38 ( context_text-Person-913 )
Entity ID: 40 ( context_text-Person-234 )
Entity ID: 49 ( context_text-Person-73 )
Entity ID: 51 ( context_text-Person-237 )
Entity ID: 53 ( context_text-Person-239 )
Entity ID: 50 ( context_text-Person-901 )
Entity ID: 52 ( context_text-Person-236 )
Entity ID: 55 ( context_text-Person-23 )
Entity ID: 57 ( context_text-Person-933 )
Entity ID: 59 ( context_text-Person-268 )
Entity ID: 60 ( context_text-Person-934 )
Entity ID: 61 ( context_text-Person-1049 )
Entity ID: 56 ( context_text-Person-935 )
Entity ID: 58 ( context_text-Person-269 )
Entity ID: 35 ( context_text-Person-178 )
Entity ID: 66 ( context_text-Person-274 )
Entity ID: 69 ( context_text-Person-69 )
Entity ID: 71 ( context_text-Person-290 )
Entity ID: 70 ( context_text-Person-289 )
Entity ID: 80 ( context_text-Person-591 )
Entity ID: 82 ( context_text-Person-311 )
Entity ID: 78 ( context_text-Person-752 )
Entity ID: 75 ( context_text-Person-753 )
Entity ID: 77 ( context_text-Person-295 )
Entity ID: 81 ( context_text-Person-312 )
Entity ID: 83 ( context_text-Person-310 )
Entity ID: 85 ( context_text-Person-84 )
Entity ID: 87 ( context_text-Person-308 )
Entity ID: 89 ( context_text-Person-307 )
Entity ID: 90 ( context_text-Person-1001 )
Entity ID: 86 ( context_text-Person-1002 )
Entity ID: 88 ( context_text-Person-1003 )
Entity ID: 92 ( context_text-Person-302 )
Entity ID: 74 ( context_text-Person-217 )
Entity ID: 76 ( context_text-Person-292 )
Entity ID: 67 ( context_text-Person-273 )
Entity ID: 96 ( context_text-Person-303 )
Entity ID: 97 ( context_text-Person-305 )
Entity ID: 93 ( context_text-Person-306 )
Entity ID: 95 ( context_text-Person-304 )
Entity ID: 118 ( context_text-Person-231 )
Entity ID: 103 ( context_text-Person-259 )
Entity ID: 100 ( context_text-Person-754 )
Entity ID: 102 ( context_text-Person-261 )
Entity ID: 105 ( context_text-Person-223 )
Entity ID: 119 ( context_text-Person-233 )
Entity ID: 107 ( context_text-Person-227 )
Entity ID: 109 ( context_text-Person-228 )
Entity ID: 110 ( context_text-Person-226 )
Entity ID: 111 ( context_text-Person-1029 )
Entity ID: 112 ( context_text-Person-224 )
Entity ID: 113 ( context_text-Person-230 )
Entity ID: 106 ( context_text-Person-225 )
Entity ID: 108 ( context_text-Person-229 )
Entity ID: 115 ( context_text-Person-222 )
Entity ID: 99 ( context_text-Person-66 )
Entity ID: 101 ( context_text-Person-263 )
Entity ID: 117 ( context_text-Person-232 )
Entity ID: 94 ( context_text-Person-1009 )
Entity ID: 139 ( context_text-Person-158 )
Entity ID: 124 ( context_text-Person-218 )
Entity ID: 121 ( context_text-Person-221 )
Entity ID: 123 ( context_text-Person-219 )
Entity ID: 127 ( context_text-Person-980 )
Entity ID: 129 ( context_text-Person-188 )
Entity ID: 130 ( context_text-Person-187 )
Entity ID: 131 ( context_text-Person-189 )
Entity ID: 126 ( context_text-Person-1043 )
Entity ID: 128 ( context_text-Person-1044 )
Entity ID: 122 ( context_text-Person-220 )
Entity ID: 140 ( context_text-Person-157 )
Entity ID: 136 ( context_text-Person-159 )
Entity ID: 138 ( context_text-Person-156 )
Entity ID: 142 ( context_text-Person-167 )
Entity ID: 144 ( context_text-Person-168 )
Entity ID: 143 ( context_text-Person-169 )
Entity ID: 145 ( context_text-Person-889 )
Entity ID: 135 ( context_text-Person-155 )
Entity ID: 148 ( context_text-Person-171 )
Entity ID: 137 ( context_text-Person-160 )
Entity ID: 174 ( context_text-Person-207 )
Entity ID: 149 ( context_text-Person-874 )
Entity ID: 169 ( context_text-Person-907 )
Entity ID: 151 ( context_text-Person-170 )
Entity ID: 153 ( context_text-Person-143 )
Entity ID: 155 ( context_text-Person-197 )
Entity ID: 161 ( context_text-Person-183 )
Entity ID: 158 ( context_text-Person-193 )
Entity ID: 163 ( context_text-Person-185 )
Entity ID: 154 ( context_text-Person-194 )
Entity ID: 156 ( context_text-Person-854 )
Entity ID: 159 ( context_text-Person-195 )
Entity ID: 162 ( context_text-Person-184 )
Entity ID: 157 ( context_text-Person-196 )
Entity ID: 165 ( context_text-Person-190 )
Entity ID: 167 ( context_text-Person-751 )
Entity ID: 166 ( context_text-Person-906 )
Entity ID: 168 ( context_text-Person-191 )
Entity ID: 171 ( context_text-Person-48 )
Entity ID: 173 ( context_text-Person-206 )
Entity ID: 175 ( context_text-Person-210 )
Entity ID: 176 ( context_text-Person-209 )
Entity ID: 150 ( context_text-Person-873 )
Entity ID: 72 ( context_text-Person-102 )
Entity ID: 172 ( context_text-Person-204 )
Entity ID: 178 ( context_text-Person-262 )
Entity ID: 180 ( context_text-Person-264 )
Entity ID: 182 ( context_text-Person-924 )
Entity ID: 183 ( context_text-Person-922 )
Entity ID: 184 ( context_text-Person-921 )
Entity ID: 185 ( context_text-Person-923 )
Entity ID: 179 ( context_text-Person-925 )
Entity ID: 181 ( context_text-Person-265 )
Entity ID: 189 ( context_text-Person-919 )
Entity ID: 188 ( context_text-Person-260 )
Entity ID: 190 ( context_text-Person-920 )
Entity ID: 192 ( context_text-Person-90 )
Entity ID: 194 ( context_text-Person-244 )
Entity ID: 196 ( context_text-Person-940 )
Entity ID: 197 ( context_text-Person-942 )
Entity ID: 198 ( context_text-Person-939 )
Entity ID: 199 ( context_text-Person-941 )
Entity ID: 200 ( context_text-Person-316 )
Entity ID: 201 ( context_text-Person-243 )
Entity ID: 193 ( context_text-Person-938 )
Entity ID: 195 ( context_text-Person-242 )
Entity ID: 147 ( context_text-Person-56 )
Entity ID: 221 ( context_text-Person-288 )
Entity ID: 206 ( context_text-Person-250 )
Entity ID: 207 ( context_text-Person-927 )
Entity ID: 203 ( context_text-Person-255 )
Entity ID: 214 ( context_text-Person-282 )
Entity ID: 205 ( context_text-Person-926 )
Entity ID: 215 ( context_text-Person-285 )
Entity ID: 209 ( context_text-Person-278 )
Entity ID: 211 ( context_text-Person-283 )
Entity ID: 213 ( context_text-Person-284 )
Entity ID: 216 ( context_text-Person-280 )
Entity ID: 217 ( context_text-Person-279 )
Entity ID: 218 ( context_text-Person-286 )
Entity ID: 210 ( context_text-Person-281 )
Entity ID: 212 ( context_text-Person-287 )
Entity ID: 204 ( context_text-Person-248 )
Entity ID: 220 ( context_text-Person-198 )
Entity ID: 228 ( context_text-Person-315 )
Entity ID: 229 ( context_text-Person-313 )
Entity ID: 225 ( context_text-Person-972 )
Entity ID: 227 ( context_text-Person-314 )
Entity ID: 224 ( context_text-Person-60 )
Entity ID: 226 ( context_text-Person-135 )
Entity ID: 222 ( context_text-Person-943 )
Entity ID: 234 ( context_text-Person-1022 )
Entity ID: 231 ( context_text-Person-1023 )
Entity ID: 233 ( context_text-Person-301 )
Entity ID: 236 ( context_text-Person-297 )
Entity ID: 238 ( context_text-Person-1018 )
Entity ID: 240 ( context_text-Person-298 )
Entity ID: 241 ( context_text-Person-1017 )
Entity ID: 242 ( context_text-Person-1019 )
Entity ID: 243 ( context_text-Person-299 )
Entity ID: 237 ( context_text-Person-1020 )
Entity ID: 239 ( context_text-Person-300 )
Entity ID: 246 ( context_text-Person-1016 )
Entity ID: 248 ( context_text-Person-905 )
Entity ID: 249 ( context_text-Person-1010 )
Entity ID: 250 ( context_text-Person-1011 )
Entity ID: 251 ( context_text-Person-1013 )
Entity ID: 252 ( context_text-Person-763 )
Entity ID: 253 ( context_text-Person-438 )
Entity ID: 254 ( context_text-Person-1012 )
Entity ID: 255 ( context_text-Person-1015 )
Entity ID: 256 ( context_text-Person-276 )
Entity ID: 245 ( context_text-Person-1014 )
Entity ID: 247 ( context_text-Person-277 )
Entity ID: 258 ( context_text-Person-293 )
Entity ID: 232 ( context_text-Person-1021 )
Entity ID: 260 ( context_text-Person-296 )
Entity ID: 262 ( context_text-Person-117 )
Entity ID: 264 ( context_text-Person-266 )
Entity ID: 263 ( context_text-Person-267 )
Entity ID: 267 ( context_text-Person-241 )
Entity ID: 269 ( context_text-Person-238 )
Entity ID: 266 ( context_text-Person-235 )
Entity ID: 268 ( context_text-Person-240 )
Entity ID: 277 ( context_text-Person-251 )
Entity ID: 272 ( context_text-Person-1025 )
Entity ID: 274 ( context_text-Person-1024 )
Entity ID: 275 ( context_text-Person-249 )
Entity ID: 276 ( context_text-Person-317 )
Entity ID: 278 ( context_text-Person-1027 )
Entity ID: 279 ( context_text-Person-755 )
Entity ID: 280 ( context_text-Person-253 )
Entity ID: 281 ( context_text-Person-247 )
Entity ID: 282 ( context_text-Person-256 )
Entity ID: 283 ( context_text-Person-245 )
Entity ID: 284 ( context_text-Person-254 )
Entity ID: 285 ( context_text-Person-246 )
Entity ID: 271 ( context_text-Person-1026 )
Entity ID: 273 ( context_text-Person-1028 )
Entity ID: 310 ( context_text-Person-1040 )
Entity ID: 291 ( context_text-Person-148 )
Entity ID: 287 ( context_text-Person-257 )
Entity ID: 315 ( context_text-Person-202 )
Entity ID: 304 ( context_text-Person-214 )
Entity ID: 289 ( context_text-Person-1033 )
Entity ID: 313 ( context_text-Person-1038 )
Entity ID: 292 ( context_text-Person-1030 )
Entity ID: 293 ( context_text-Person-1031 )
Entity ID: 294 ( context_text-Person-1032 )
Entity ID: 295 ( context_text-Person-1036 )
Entity ID: 296 ( context_text-Person-1035 )
Entity ID: 288 ( context_text-Person-1034 )
Entity ID: 290 ( context_text-Person-1037 )
Entity ID: 298 ( context_text-Person-592 )
Entity ID: 300 ( context_text-Person-756 )
Entity ID: 299 ( context_text-Person-1046 )
Entity ID: 301 ( context_text-Person-1045 )
Entity ID: 303 ( context_text-Person-211 )
Entity ID: 305 ( context_text-Person-216 )
Entity ID: 306 ( context_text-Person-212 )
Entity ID: 309 ( context_text-Person-1039 )
Entity ID: 311 ( context_text-Person-1042 )
Entity ID: 308 ( context_text-Person-1041 )
Entity ID: 314 ( context_text-Person-203 )
Entity ID: 259 ( context_text-Person-294 )
Entity ID: 316 ( context_text-Person-201 )
Entity ID: 317 ( context_text-Person-200 )
Entity ID: 318 ( context_text-Person-199 )
Entity ID: 65 ( context_text-Person-275 )
Entity ID: 319 ( context_text-Person-205 )

In [25]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 2316 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 28, 30, 13, 14, 15, 16, 27, 10, 12, 18, 20, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 9, 11, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) )

TO - result count: 3162 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 28, 30, 13, 14, 15, 16, 27, 10, 12, 18, 20, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 9, 11, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) )

THROUGH - result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 28, 30, 13, 14, 15, 16, 27, 10, 12, 18, 20, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 9, 11, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) )

FROM | TO - result count: 3162 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 28, 30, 13, 14, 15, 16, 27, 10, 12, 18, 20, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 9, 11, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 28, 30, 13, 14, 15, 16, 27, 10, 12, 18, 20, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 9, 11, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319))) )

FROM | TO | THROUGH - result count: 3162 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 28, 30, 13, 14, 15, 16, 27, 10, 12, 18, 20, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 9, 11, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 28, 30, 13, 14, 15, 16, 27, 10, 12, 18, 20, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 9, 11, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (8, 28, 30, 13, 14, 15, 16, 27, 10, 12, 18, 20, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 9, 11, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319))) )

In [26]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "46" )
test_value_list.append( "163" )
test_value_list.append( "161" )
test_value_list.append( "164" )
test_value_list.append( "30" )
test_value_list.append( "175" )

id_name_q = Q( entity_identifier__name = "person_sourcenet_id" )
id_value_q = Q( entity_identifier__uuid__in = test_value_list )
combined_q = id_name_q & ( ~ id_value_q )

#entity_qs = entity_qs.filter( id_name_q )
#entity_qs = entity_qs.exclude( id_value_q )
entity_qs = entity_qs.filter( combined_q )

print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 259 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_identifier" ON ("context_entity"."id" = "context_entity_identifier"."entity_id") WHERE ("context_entity_identifier"."name" = person_sourcenet_id AND NOT ("context_entity"."id" IN (SELECT U1."entity_id" FROM "context_entity_identifier" U1 WHERE (U1."uuid" IN (46, 163, 161, 164, 30, 175) AND U1."id" = ("context_entity_identifier"."id"))))))
Entity ID: 28 ( context_text-Person-182 )
Entity ID: 30 ( context_text-Person-180 )
Entity ID: 13 ( context_text-Person-166 )
Entity ID: 14 ( context_text-Person-186 )
Entity ID: 15 ( context_text-Person-162 )
Entity ID: 27 ( context_text-Person-877 )
Entity ID: 10 ( context_text-Person-872 )
Entity ID: 12 ( context_text-Person-165 )
Entity ID: 22 ( context_text-Person-176 )
Entity ID: 23 ( context_text-Person-173 )
Entity ID: 24 ( context_text-Person-172 )
Entity ID: 25 ( context_text-Person-32 )
Entity ID: 19 ( context_text-Person-750 )
Entity ID: 21 ( context_text-Person-174 )
Entity ID: 31 ( context_text-Person-876 )
Entity ID: 32 ( context_text-Person-181 )
Entity ID: 33 ( context_text-Person-875 )
Entity ID: 29 ( context_text-Person-878 )
Entity ID: 63 ( context_text-Person-36 )
Entity ID: 64 ( context_text-Person-272 )
Entity ID: 36 ( context_text-Person-179 )
Entity ID: 39 ( context_text-Person-914 )
Entity ID: 41 ( context_text-Person-909 )
Entity ID: 42 ( context_text-Person-213 )
Entity ID: 43 ( context_text-Person-910 )
Entity ID: 44 ( context_text-Person-132 )
Entity ID: 45 ( context_text-Person-912 )
Entity ID: 46 ( context_text-Person-215 )
Entity ID: 47 ( context_text-Person-911 )
Entity ID: 38 ( context_text-Person-913 )
Entity ID: 40 ( context_text-Person-234 )
Entity ID: 49 ( context_text-Person-73 )
Entity ID: 51 ( context_text-Person-237 )
Entity ID: 53 ( context_text-Person-239 )
Entity ID: 50 ( context_text-Person-901 )
Entity ID: 52 ( context_text-Person-236 )
Entity ID: 55 ( context_text-Person-23 )
Entity ID: 57 ( context_text-Person-933 )
Entity ID: 59 ( context_text-Person-268 )
Entity ID: 60 ( context_text-Person-934 )
Entity ID: 61 ( context_text-Person-1049 )
Entity ID: 56 ( context_text-Person-935 )
Entity ID: 58 ( context_text-Person-269 )
Entity ID: 35 ( context_text-Person-178 )
Entity ID: 66 ( context_text-Person-274 )
Entity ID: 69 ( context_text-Person-69 )
Entity ID: 71 ( context_text-Person-290 )
Entity ID: 70 ( context_text-Person-289 )
Entity ID: 80 ( context_text-Person-591 )
Entity ID: 82 ( context_text-Person-311 )
Entity ID: 78 ( context_text-Person-752 )
Entity ID: 75 ( context_text-Person-753 )
Entity ID: 77 ( context_text-Person-295 )
Entity ID: 81 ( context_text-Person-312 )
Entity ID: 83 ( context_text-Person-310 )
Entity ID: 85 ( context_text-Person-84 )
Entity ID: 87 ( context_text-Person-308 )
Entity ID: 89 ( context_text-Person-307 )
Entity ID: 90 ( context_text-Person-1001 )
Entity ID: 86 ( context_text-Person-1002 )
Entity ID: 88 ( context_text-Person-1003 )
Entity ID: 92 ( context_text-Person-302 )
Entity ID: 74 ( context_text-Person-217 )
Entity ID: 76 ( context_text-Person-292 )
Entity ID: 67 ( context_text-Person-273 )
Entity ID: 96 ( context_text-Person-303 )
Entity ID: 97 ( context_text-Person-305 )
Entity ID: 93 ( context_text-Person-306 )
Entity ID: 95 ( context_text-Person-304 )
Entity ID: 118 ( context_text-Person-231 )
Entity ID: 103 ( context_text-Person-259 )
Entity ID: 100 ( context_text-Person-754 )
Entity ID: 102 ( context_text-Person-261 )
Entity ID: 105 ( context_text-Person-223 )
Entity ID: 119 ( context_text-Person-233 )
Entity ID: 107 ( context_text-Person-227 )
Entity ID: 109 ( context_text-Person-228 )
Entity ID: 110 ( context_text-Person-226 )
Entity ID: 111 ( context_text-Person-1029 )
Entity ID: 112 ( context_text-Person-224 )
Entity ID: 113 ( context_text-Person-230 )
Entity ID: 106 ( context_text-Person-225 )
Entity ID: 108 ( context_text-Person-229 )
Entity ID: 115 ( context_text-Person-222 )
Entity ID: 99 ( context_text-Person-66 )
Entity ID: 101 ( context_text-Person-263 )
Entity ID: 117 ( context_text-Person-232 )
Entity ID: 94 ( context_text-Person-1009 )
Entity ID: 139 ( context_text-Person-158 )
Entity ID: 124 ( context_text-Person-218 )
Entity ID: 121 ( context_text-Person-221 )
Entity ID: 123 ( context_text-Person-219 )
Entity ID: 127 ( context_text-Person-980 )
Entity ID: 129 ( context_text-Person-188 )
Entity ID: 130 ( context_text-Person-187 )
Entity ID: 131 ( context_text-Person-189 )
Entity ID: 126 ( context_text-Person-1043 )
Entity ID: 128 ( context_text-Person-1044 )
Entity ID: 122 ( context_text-Person-220 )
Entity ID: 140 ( context_text-Person-157 )
Entity ID: 136 ( context_text-Person-159 )
Entity ID: 138 ( context_text-Person-156 )
Entity ID: 142 ( context_text-Person-167 )
Entity ID: 144 ( context_text-Person-168 )
Entity ID: 143 ( context_text-Person-169 )
Entity ID: 145 ( context_text-Person-889 )
Entity ID: 135 ( context_text-Person-155 )
Entity ID: 148 ( context_text-Person-171 )
Entity ID: 137 ( context_text-Person-160 )
Entity ID: 174 ( context_text-Person-207 )
Entity ID: 149 ( context_text-Person-874 )
Entity ID: 169 ( context_text-Person-907 )
Entity ID: 151 ( context_text-Person-170 )
Entity ID: 153 ( context_text-Person-143 )
Entity ID: 155 ( context_text-Person-197 )
Entity ID: 161 ( context_text-Person-183 )
Entity ID: 158 ( context_text-Person-193 )
Entity ID: 163 ( context_text-Person-185 )
Entity ID: 154 ( context_text-Person-194 )
Entity ID: 156 ( context_text-Person-854 )
Entity ID: 159 ( context_text-Person-195 )
Entity ID: 162 ( context_text-Person-184 )
Entity ID: 157 ( context_text-Person-196 )
Entity ID: 165 ( context_text-Person-190 )
Entity ID: 167 ( context_text-Person-751 )
Entity ID: 166 ( context_text-Person-906 )
Entity ID: 168 ( context_text-Person-191 )
Entity ID: 171 ( context_text-Person-48 )
Entity ID: 173 ( context_text-Person-206 )
Entity ID: 175 ( context_text-Person-210 )
Entity ID: 176 ( context_text-Person-209 )
Entity ID: 150 ( context_text-Person-873 )
Entity ID: 72 ( context_text-Person-102 )
Entity ID: 172 ( context_text-Person-204 )
Entity ID: 178 ( context_text-Person-262 )
Entity ID: 180 ( context_text-Person-264 )
Entity ID: 182 ( context_text-Person-924 )
Entity ID: 183 ( context_text-Person-922 )
Entity ID: 184 ( context_text-Person-921 )
Entity ID: 185 ( context_text-Person-923 )
Entity ID: 179 ( context_text-Person-925 )
Entity ID: 181 ( context_text-Person-265 )
Entity ID: 189 ( context_text-Person-919 )
Entity ID: 188 ( context_text-Person-260 )
Entity ID: 190 ( context_text-Person-920 )
Entity ID: 192 ( context_text-Person-90 )
Entity ID: 194 ( context_text-Person-244 )
Entity ID: 196 ( context_text-Person-940 )
Entity ID: 197 ( context_text-Person-942 )
Entity ID: 198 ( context_text-Person-939 )
Entity ID: 199 ( context_text-Person-941 )
Entity ID: 200 ( context_text-Person-316 )
Entity ID: 201 ( context_text-Person-243 )
Entity ID: 193 ( context_text-Person-938 )
Entity ID: 195 ( context_text-Person-242 )
Entity ID: 147 ( context_text-Person-56 )
Entity ID: 221 ( context_text-Person-288 )
Entity ID: 206 ( context_text-Person-250 )
Entity ID: 207 ( context_text-Person-927 )
Entity ID: 203 ( context_text-Person-255 )
Entity ID: 214 ( context_text-Person-282 )
Entity ID: 205 ( context_text-Person-926 )
Entity ID: 215 ( context_text-Person-285 )
Entity ID: 209 ( context_text-Person-278 )
Entity ID: 211 ( context_text-Person-283 )
Entity ID: 213 ( context_text-Person-284 )
Entity ID: 216 ( context_text-Person-280 )
Entity ID: 217 ( context_text-Person-279 )
Entity ID: 218 ( context_text-Person-286 )
Entity ID: 210 ( context_text-Person-281 )
Entity ID: 212 ( context_text-Person-287 )
Entity ID: 204 ( context_text-Person-248 )
Entity ID: 220 ( context_text-Person-198 )
Entity ID: 228 ( context_text-Person-315 )
Entity ID: 229 ( context_text-Person-313 )
Entity ID: 225 ( context_text-Person-972 )
Entity ID: 227 ( context_text-Person-314 )
Entity ID: 224 ( context_text-Person-60 )
Entity ID: 226 ( context_text-Person-135 )
Entity ID: 222 ( context_text-Person-943 )
Entity ID: 234 ( context_text-Person-1022 )
Entity ID: 231 ( context_text-Person-1023 )
Entity ID: 233 ( context_text-Person-301 )
Entity ID: 236 ( context_text-Person-297 )
Entity ID: 238 ( context_text-Person-1018 )
Entity ID: 240 ( context_text-Person-298 )
Entity ID: 241 ( context_text-Person-1017 )
Entity ID: 242 ( context_text-Person-1019 )
Entity ID: 243 ( context_text-Person-299 )
Entity ID: 237 ( context_text-Person-1020 )
Entity ID: 239 ( context_text-Person-300 )
Entity ID: 246 ( context_text-Person-1016 )
Entity ID: 248 ( context_text-Person-905 )
Entity ID: 249 ( context_text-Person-1010 )
Entity ID: 250 ( context_text-Person-1011 )
Entity ID: 251 ( context_text-Person-1013 )
Entity ID: 252 ( context_text-Person-763 )
Entity ID: 253 ( context_text-Person-438 )
Entity ID: 254 ( context_text-Person-1012 )
Entity ID: 255 ( context_text-Person-1015 )
Entity ID: 256 ( context_text-Person-276 )
Entity ID: 245 ( context_text-Person-1014 )
Entity ID: 247 ( context_text-Person-277 )
Entity ID: 258 ( context_text-Person-293 )
Entity ID: 232 ( context_text-Person-1021 )
Entity ID: 260 ( context_text-Person-296 )
Entity ID: 262 ( context_text-Person-117 )
Entity ID: 264 ( context_text-Person-266 )
Entity ID: 263 ( context_text-Person-267 )
Entity ID: 267 ( context_text-Person-241 )
Entity ID: 269 ( context_text-Person-238 )
Entity ID: 266 ( context_text-Person-235 )
Entity ID: 268 ( context_text-Person-240 )
Entity ID: 277 ( context_text-Person-251 )
Entity ID: 272 ( context_text-Person-1025 )
Entity ID: 274 ( context_text-Person-1024 )
Entity ID: 275 ( context_text-Person-249 )
Entity ID: 276 ( context_text-Person-317 )
Entity ID: 278 ( context_text-Person-1027 )
Entity ID: 279 ( context_text-Person-755 )
Entity ID: 280 ( context_text-Person-253 )
Entity ID: 281 ( context_text-Person-247 )
Entity ID: 282 ( context_text-Person-256 )
Entity ID: 283 ( context_text-Person-245 )
Entity ID: 284 ( context_text-Person-254 )
Entity ID: 285 ( context_text-Person-246 )
Entity ID: 271 ( context_text-Person-1026 )
Entity ID: 273 ( context_text-Person-1028 )
Entity ID: 310 ( context_text-Person-1040 )
Entity ID: 291 ( context_text-Person-148 )
Entity ID: 287 ( context_text-Person-257 )
Entity ID: 315 ( context_text-Person-202 )
Entity ID: 304 ( context_text-Person-214 )
Entity ID: 289 ( context_text-Person-1033 )
Entity ID: 313 ( context_text-Person-1038 )
Entity ID: 292 ( context_text-Person-1030 )
Entity ID: 293 ( context_text-Person-1031 )
Entity ID: 294 ( context_text-Person-1032 )
Entity ID: 295 ( context_text-Person-1036 )
Entity ID: 296 ( context_text-Person-1035 )
Entity ID: 288 ( context_text-Person-1034 )
Entity ID: 290 ( context_text-Person-1037 )
Entity ID: 298 ( context_text-Person-592 )
Entity ID: 300 ( context_text-Person-756 )
Entity ID: 299 ( context_text-Person-1046 )
Entity ID: 301 ( context_text-Person-1045 )
Entity ID: 303 ( context_text-Person-211 )
Entity ID: 305 ( context_text-Person-216 )
Entity ID: 306 ( context_text-Person-212 )
Entity ID: 309 ( context_text-Person-1039 )
Entity ID: 311 ( context_text-Person-1042 )
Entity ID: 308 ( context_text-Person-1041 )
Entity ID: 314 ( context_text-Person-203 )
Entity ID: 259 ( context_text-Person-294 )
Entity ID: 316 ( context_text-Person-201 )
Entity ID: 317 ( context_text-Person-200 )
Entity ID: 318 ( context_text-Person-199 )
Entity ID: 65 ( context_text-Person-275 )
Entity ID: 319 ( context_text-Person-205 )

In [27]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 2228 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (28, 30, 13, 14, 15, 27, 10, 12, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) )

TO - result count: 3099 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (28, 30, 13, 14, 15, 27, 10, 12, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) )

THROUGH - result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (28, 30, 13, 14, 15, 27, 10, 12, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) )

FROM | TO - result count: 3124 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (28, 30, 13, 14, 15, 27, 10, 12, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (28, 30, 13, 14, 15, 27, 10, 12, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319))) )

FROM | TO | THROUGH - result count: 3124 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (28, 30, 13, 14, 15, 27, 10, 12, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (28, 30, 13, 14, 15, 27, 10, 12, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (28, 30, 13, 14, 15, 27, 10, 12, 22, 23, 24, 25, 19, 21, 31, 32, 33, 29, 63, 64, 36, 39, 41, 42, 43, 44, 45, 46, 47, 38, 40, 49, 51, 53, 50, 52, 55, 57, 59, 60, 61, 56, 58, 35, 66, 69, 71, 70, 80, 82, 78, 75, 77, 81, 83, 85, 87, 89, 90, 86, 88, 92, 74, 76, 67, 96, 97, 93, 95, 118, 103, 100, 102, 105, 119, 107, 109, 110, 111, 112, 113, 106, 108, 115, 99, 101, 117, 94, 139, 124, 121, 123, 127, 129, 130, 131, 126, 128, 122, 140, 136, 138, 142, 144, 143, 145, 135, 148, 137, 174, 149, 169, 151, 153, 155, 161, 158, 163, 154, 156, 159, 162, 157, 165, 167, 166, 168, 171, 173, 175, 176, 150, 72, 172, 178, 180, 182, 183, 184, 185, 179, 181, 189, 188, 190, 192, 194, 196, 197, 198, 199, 200, 201, 193, 195, 147, 221, 206, 207, 203, 214, 205, 215, 209, 211, 213, 216, 217, 218, 210, 212, 204, 220, 228, 229, 225, 227, 224, 226, 222, 234, 231, 233, 236, 238, 240, 241, 242, 243, 237, 239, 246, 248, 249, 250, 251, 252, 253, 254, 255, 256, 245, 247, 258, 232, 260, 262, 264, 263, 267, 269, 266, 268, 277, 272, 274, 275, 276, 278, 279, 280, 281, 282, 283, 284, 285, 271, 273, 310, 291, 287, 315, 304, 289, 313, 292, 293, 294, 295, 296, 288, 290, 298, 300, 299, 301, 303, 305, 306, 309, 311, 308, 314, 259, 316, 317, 318, 65, 319))) )

filter on entity traits

Figure out which relation roles we are focusing on (combination of FROM, TO, THROUGH), then build out query to properly filter.

filter just entities based on entity traits


In [48]:
# try to just find the entity with the desired identifier.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
entity_qs = entity_qs.filter( entity_trait__name = "first_name" )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
entity_qs = entity_qs.filter( entity_trait__value = "John" )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 265 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_trait" ON ("context_entity"."id" = "context_entity_trait"."entity_id") WHERE "context_entity_trait"."name" = first_name)

result count: 9 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_trait" ON ("context_entity"."id" = "context_entity_trait"."entity_id") INNER JOIN "context_entity_trait" T3 ON ("context_entity"."id" = T3."entity_id") WHERE ("context_entity_trait"."name" = first_name AND T3."value" = John))

In [55]:
# try to just find the entity with the desired identifier.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
combined_q = Q( entity_trait__name = "first_name" ) & Q( entity_trait__value = "Larry" )
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 1 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_trait" ON ("context_entity"."id" = "context_entity_trait"."entity_id") WHERE ("context_entity_trait"."name" = first_name AND "context_entity_trait"."value" = Larry))

filter relations based on entity traits


In [50]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 68 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" = John)) )

TO - result count: 107 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" = John)) )

THROUGH - result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" = John)) )

FROM | TO - result count: 175 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" = John)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" = John))) )

FROM | TO | THROUGH - result count: 175 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" = John)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" = John)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" = John))) )

test IN list of entity traits


In [57]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "John" )
test_value_list.append( "Michael" )
test_value_list.append( "Robert" )
test_value_list.append( "Steve" )
test_value_list.append( "Larry" )

trait_name_q = Q( entity_trait__name = "first_name" )
trait_value_q = Q( entity_trait__value__in = test_value_list )
combined_q = trait_name_q & trait_value_q
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 20 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_trait" ON ("context_entity"."id" = "context_entity_trait"."entity_id") WHERE ("context_entity_trait"."name" = first_name AND "context_entity_trait"."value" IN (John, Michael, Robert, Steve, Larry)))
Entity ID: 12 ( context_text-Person-165 )
Entity ID: 21 ( context_text-Person-174 )
Entity ID: 44 ( context_text-Person-132 )
Entity ID: 70 ( context_text-Person-289 )
Entity ID: 81 ( context_text-Person-312 )
Entity ID: 85 ( context_text-Person-84 )
Entity ID: 110 ( context_text-Person-226 )
Entity ID: 115 ( context_text-Person-222 )
Entity ID: 139 ( context_text-Person-158 )
Entity ID: 150 ( context_text-Person-873 )
Entity ID: 178 ( context_text-Person-262 )
Entity ID: 183 ( context_text-Person-922 )
Entity ID: 188 ( context_text-Person-260 )
Entity ID: 218 ( context_text-Person-286 )
Entity ID: 229 ( context_text-Person-313 )
Entity ID: 237 ( context_text-Person-1020 )
Entity ID: 264 ( context_text-Person-266 )
Entity ID: 272 ( context_text-Person-1025 )
Entity ID: 309 ( context_text-Person-1039 )
Entity ID: 310 ( context_text-Person-1040 )

In [58]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 139 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" IN (John, Michael, Robert, Steve, Larry))) )

TO - result count: 218 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" IN (John, Michael, Robert, Steve, Larry))) )

THROUGH - result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" IN (John, Michael, Robert, Steve, Larry))) )

FROM | TO - result count: 353 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" IN (John, Michael, Robert, Steve, Larry))) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" IN (John, Michael, Robert, Steve, Larry)))) )

FROM | TO | THROUGH - result count: 353 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" IN (John, Michael, Robert, Steve, Larry))) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" IN (John, Michael, Robert, Steve, Larry))) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = first_name AND U1."value" IN (John, Michael, Robert, Steve, Larry)))) )

test NOT IN list of entity traits


In [61]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "20202020" )
test_value_list.append( "20202021" )
test_value_list.append( "20202022" )

trait_name_q = Q( entity_trait__name = "first_name" )
trait_value_q = ~ Q( entity_trait__value__in = test_value_list )
combined_q = trait_name_q & trait_value_q
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 265 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_trait" ON ("context_entity"."id" = "context_entity_trait"."entity_id") WHERE ("context_entity_trait"."name" = first_name AND NOT ("context_entity"."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = ("context_entity_trait"."id"))))))
Entity ID: 8 ( context_text-Person-161 )
Entity ID: 9 ( context_text-Person-46 )
Entity ID: 11 ( context_text-Person-164 )
Entity ID: 12 ( context_text-Person-165 )
Entity ID: 13 ( context_text-Person-166 )
Entity ID: 14 ( context_text-Person-186 )
Entity ID: 10 ( context_text-Person-872 )
Entity ID: 16 ( context_text-Person-163 )
Entity ID: 15 ( context_text-Person-162 )
Entity ID: 18 ( context_text-Person-30 )
Entity ID: 20 ( context_text-Person-175 )
Entity ID: 21 ( context_text-Person-174 )
Entity ID: 22 ( context_text-Person-176 )
Entity ID: 23 ( context_text-Person-173 )
Entity ID: 19 ( context_text-Person-750 )
Entity ID: 24 ( context_text-Person-172 )
Entity ID: 25 ( context_text-Person-32 )
Entity ID: 28 ( context_text-Person-182 )
Entity ID: 29 ( context_text-Person-878 )
Entity ID: 30 ( context_text-Person-180 )
Entity ID: 31 ( context_text-Person-876 )
Entity ID: 32 ( context_text-Person-181 )
Entity ID: 27 ( context_text-Person-877 )
Entity ID: 33 ( context_text-Person-875 )
Entity ID: 35 ( context_text-Person-178 )
Entity ID: 36 ( context_text-Person-179 )
Entity ID: 39 ( context_text-Person-914 )
Entity ID: 40 ( context_text-Person-234 )
Entity ID: 41 ( context_text-Person-909 )
Entity ID: 42 ( context_text-Person-213 )
Entity ID: 43 ( context_text-Person-910 )
Entity ID: 44 ( context_text-Person-132 )
Entity ID: 45 ( context_text-Person-912 )
Entity ID: 46 ( context_text-Person-215 )
Entity ID: 47 ( context_text-Person-911 )
Entity ID: 49 ( context_text-Person-73 )
Entity ID: 51 ( context_text-Person-237 )
Entity ID: 52 ( context_text-Person-236 )
Entity ID: 53 ( context_text-Person-239 )
Entity ID: 50 ( context_text-Person-901 )
Entity ID: 55 ( context_text-Person-23 )
Entity ID: 57 ( context_text-Person-933 )
Entity ID: 58 ( context_text-Person-269 )
Entity ID: 60 ( context_text-Person-934 )
Entity ID: 61 ( context_text-Person-1049 )
Entity ID: 59 ( context_text-Person-268 )
Entity ID: 165 ( context_text-Person-190 )
Entity ID: 65 ( context_text-Person-275 )
Entity ID: 66 ( context_text-Person-274 )
Entity ID: 67 ( context_text-Person-273 )
Entity ID: 63 ( context_text-Person-36 )
Entity ID: 64 ( context_text-Person-272 )
Entity ID: 72 ( context_text-Person-102 )
Entity ID: 69 ( context_text-Person-69 )
Entity ID: 71 ( context_text-Person-290 )
Entity ID: 70 ( context_text-Person-289 )
Entity ID: 76 ( context_text-Person-292 )
Entity ID: 74 ( context_text-Person-217 )
Entity ID: 77 ( context_text-Person-295 )
Entity ID: 78 ( context_text-Person-752 )
Entity ID: 75 ( context_text-Person-753 )
Entity ID: 82 ( context_text-Person-311 )
Entity ID: 83 ( context_text-Person-310 )
Entity ID: 81 ( context_text-Person-312 )
Entity ID: 85 ( context_text-Person-84 )
Entity ID: 80 ( context_text-Person-591 )
Entity ID: 87 ( context_text-Person-308 )
Entity ID: 88 ( context_text-Person-1003 )
Entity ID: 89 ( context_text-Person-307 )
Entity ID: 90 ( context_text-Person-1001 )
Entity ID: 92 ( context_text-Person-302 )
Entity ID: 94 ( context_text-Person-1009 )
Entity ID: 93 ( context_text-Person-306 )
Entity ID: 95 ( context_text-Person-304 )
Entity ID: 96 ( context_text-Person-303 )
Entity ID: 97 ( context_text-Person-305 )
Entity ID: 101 ( context_text-Person-263 )
Entity ID: 102 ( context_text-Person-261 )
Entity ID: 103 ( context_text-Person-259 )
Entity ID: 99 ( context_text-Person-66 )
Entity ID: 105 ( context_text-Person-223 )
Entity ID: 107 ( context_text-Person-227 )
Entity ID: 108 ( context_text-Person-229 )
Entity ID: 109 ( context_text-Person-228 )
Entity ID: 110 ( context_text-Person-226 )
Entity ID: 111 ( context_text-Person-1029 )
Entity ID: 106 ( context_text-Person-225 )
Entity ID: 112 ( context_text-Person-224 )
Entity ID: 113 ( context_text-Person-230 )
Entity ID: 115 ( context_text-Person-222 )
Entity ID: 118 ( context_text-Person-231 )
Entity ID: 119 ( context_text-Person-233 )
Entity ID: 117 ( context_text-Person-232 )
Entity ID: 121 ( context_text-Person-221 )
Entity ID: 122 ( context_text-Person-220 )
Entity ID: 123 ( context_text-Person-219 )
Entity ID: 124 ( context_text-Person-218 )
Entity ID: 127 ( context_text-Person-980 )
Entity ID: 128 ( context_text-Person-1044 )
Entity ID: 129 ( context_text-Person-188 )
Entity ID: 130 ( context_text-Person-187 )
Entity ID: 126 ( context_text-Person-1043 )
Entity ID: 131 ( context_text-Person-189 )
Entity ID: 137 ( context_text-Person-160 )
Entity ID: 138 ( context_text-Person-156 )
Entity ID: 139 ( context_text-Person-158 )
Entity ID: 136 ( context_text-Person-159 )
Entity ID: 140 ( context_text-Person-157 )
Entity ID: 142 ( context_text-Person-167 )
Entity ID: 144 ( context_text-Person-168 )
Entity ID: 145 ( context_text-Person-889 )
Entity ID: 143 ( context_text-Person-169 )
Entity ID: 148 ( context_text-Person-171 )
Entity ID: 149 ( context_text-Person-874 )
Entity ID: 150 ( context_text-Person-873 )
Entity ID: 151 ( context_text-Person-170 )
Entity ID: 153 ( context_text-Person-143 )
Entity ID: 155 ( context_text-Person-197 )
Entity ID: 156 ( context_text-Person-854 )
Entity ID: 162 ( context_text-Person-184 )
Entity ID: 159 ( context_text-Person-195 )
Entity ID: 154 ( context_text-Person-194 )
Entity ID: 161 ( context_text-Person-183 )
Entity ID: 163 ( context_text-Person-185 )
Entity ID: 158 ( context_text-Person-193 )
Entity ID: 167 ( context_text-Person-751 )
Entity ID: 168 ( context_text-Person-191 )
Entity ID: 166 ( context_text-Person-906 )
Entity ID: 169 ( context_text-Person-907 )
Entity ID: 171 ( context_text-Person-48 )
Entity ID: 173 ( context_text-Person-206 )
Entity ID: 174 ( context_text-Person-207 )
Entity ID: 175 ( context_text-Person-210 )
Entity ID: 176 ( context_text-Person-209 )
Entity ID: 172 ( context_text-Person-204 )
Entity ID: 178 ( context_text-Person-262 )
Entity ID: 180 ( context_text-Person-264 )
Entity ID: 181 ( context_text-Person-265 )
Entity ID: 182 ( context_text-Person-924 )
Entity ID: 183 ( context_text-Person-922 )
Entity ID: 184 ( context_text-Person-921 )
Entity ID: 185 ( context_text-Person-923 )
Entity ID: 179 ( context_text-Person-925 )
Entity ID: 190 ( context_text-Person-920 )
Entity ID: 188 ( context_text-Person-260 )
Entity ID: 189 ( context_text-Person-919 )
Entity ID: 192 ( context_text-Person-90 )
Entity ID: 194 ( context_text-Person-244 )
Entity ID: 195 ( context_text-Person-242 )
Entity ID: 193 ( context_text-Person-938 )
Entity ID: 197 ( context_text-Person-942 )
Entity ID: 198 ( context_text-Person-939 )
Entity ID: 199 ( context_text-Person-941 )
Entity ID: 200 ( context_text-Person-316 )
Entity ID: 201 ( context_text-Person-243 )
Entity ID: 204 ( context_text-Person-248 )
Entity ID: 147 ( context_text-Person-56 )
Entity ID: 196 ( context_text-Person-940 )
Entity ID: 205 ( context_text-Person-926 )
Entity ID: 206 ( context_text-Person-250 )
Entity ID: 207 ( context_text-Person-927 )
Entity ID: 203 ( context_text-Person-255 )
Entity ID: 209 ( context_text-Person-278 )
Entity ID: 211 ( context_text-Person-283 )
Entity ID: 212 ( context_text-Person-287 )
Entity ID: 157 ( context_text-Person-196 )
Entity ID: 213 ( context_text-Person-284 )
Entity ID: 214 ( context_text-Person-282 )
Entity ID: 215 ( context_text-Person-285 )
Entity ID: 216 ( context_text-Person-280 )
Entity ID: 217 ( context_text-Person-279 )
Entity ID: 218 ( context_text-Person-286 )
Entity ID: 210 ( context_text-Person-281 )
Entity ID: 220 ( context_text-Person-198 )
Entity ID: 221 ( context_text-Person-288 )
Entity ID: 222 ( context_text-Person-943 )
Entity ID: 226 ( context_text-Person-135 )
Entity ID: 227 ( context_text-Person-314 )
Entity ID: 228 ( context_text-Person-315 )
Entity ID: 229 ( context_text-Person-313 )
Entity ID: 224 ( context_text-Person-60 )
Entity ID: 225 ( context_text-Person-972 )
Entity ID: 232 ( context_text-Person-1021 )
Entity ID: 233 ( context_text-Person-301 )
Entity ID: 234 ( context_text-Person-1022 )
Entity ID: 231 ( context_text-Person-1023 )
Entity ID: 236 ( context_text-Person-297 )
Entity ID: 238 ( context_text-Person-1018 )
Entity ID: 239 ( context_text-Person-300 )
Entity ID: 237 ( context_text-Person-1020 )
Entity ID: 240 ( context_text-Person-298 )
Entity ID: 241 ( context_text-Person-1017 )
Entity ID: 242 ( context_text-Person-1019 )
Entity ID: 243 ( context_text-Person-299 )
Entity ID: 246 ( context_text-Person-1016 )
Entity ID: 247 ( context_text-Person-277 )
Entity ID: 248 ( context_text-Person-905 )
Entity ID: 245 ( context_text-Person-1014 )
Entity ID: 249 ( context_text-Person-1010 )
Entity ID: 250 ( context_text-Person-1011 )
Entity ID: 251 ( context_text-Person-1013 )
Entity ID: 252 ( context_text-Person-763 )
Entity ID: 253 ( context_text-Person-438 )
Entity ID: 254 ( context_text-Person-1012 )
Entity ID: 255 ( context_text-Person-1015 )
Entity ID: 256 ( context_text-Person-276 )
Entity ID: 258 ( context_text-Person-293 )
Entity ID: 260 ( context_text-Person-296 )
Entity ID: 259 ( context_text-Person-294 )
Entity ID: 262 ( context_text-Person-117 )
Entity ID: 264 ( context_text-Person-266 )
Entity ID: 263 ( context_text-Person-267 )
Entity ID: 266 ( context_text-Person-235 )
Entity ID: 267 ( context_text-Person-241 )
Entity ID: 268 ( context_text-Person-240 )
Entity ID: 269 ( context_text-Person-238 )
Entity ID: 135 ( context_text-Person-155 )
Entity ID: 272 ( context_text-Person-1025 )
Entity ID: 273 ( context_text-Person-1028 )
Entity ID: 274 ( context_text-Person-1024 )
Entity ID: 276 ( context_text-Person-317 )
Entity ID: 277 ( context_text-Person-251 )
Entity ID: 278 ( context_text-Person-1027 )
Entity ID: 279 ( context_text-Person-755 )
Entity ID: 280 ( context_text-Person-253 )
Entity ID: 281 ( context_text-Person-247 )
Entity ID: 282 ( context_text-Person-256 )
Entity ID: 275 ( context_text-Person-249 )
Entity ID: 283 ( context_text-Person-245 )
Entity ID: 284 ( context_text-Person-254 )
Entity ID: 285 ( context_text-Person-246 )
Entity ID: 271 ( context_text-Person-1026 )
Entity ID: 287 ( context_text-Person-257 )
Entity ID: 289 ( context_text-Person-1033 )
Entity ID: 290 ( context_text-Person-1037 )
Entity ID: 291 ( context_text-Person-148 )
Entity ID: 292 ( context_text-Person-1030 )
Entity ID: 293 ( context_text-Person-1031 )
Entity ID: 294 ( context_text-Person-1032 )
Entity ID: 295 ( context_text-Person-1036 )
Entity ID: 296 ( context_text-Person-1035 )
Entity ID: 288 ( context_text-Person-1034 )
Entity ID: 298 ( context_text-Person-592 )
Entity ID: 301 ( context_text-Person-1045 )
Entity ID: 299 ( context_text-Person-1046 )
Entity ID: 300 ( context_text-Person-756 )
Entity ID: 315 ( context_text-Person-202 )
Entity ID: 303 ( context_text-Person-211 )
Entity ID: 305 ( context_text-Person-216 )
Entity ID: 306 ( context_text-Person-212 )
Entity ID: 304 ( context_text-Person-214 )
Entity ID: 308 ( context_text-Person-1041 )
Entity ID: 311 ( context_text-Person-1042 )
Entity ID: 310 ( context_text-Person-1040 )
Entity ID: 316 ( context_text-Person-201 )
Entity ID: 317 ( context_text-Person-200 )
Entity ID: 318 ( context_text-Person-199 )
Entity ID: 319 ( context_text-Person-205 )
Entity ID: 309 ( context_text-Person-1039 )
Entity ID: 86 ( context_text-Person-1002 )
Entity ID: 100 ( context_text-Person-754 )
Entity ID: 313 ( context_text-Person-1038 )
Entity ID: 314 ( context_text-Person-203 )
Entity ID: 38 ( context_text-Person-913 )
Entity ID: 56 ( context_text-Person-935 )

In [62]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 2316 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = (V1."id")))))) )

TO - result count: 3162 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = (V1."id")))))) )

THROUGH - result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = (V1."id")))))) )

FROM | TO - result count: 3162 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = (V1."id")))))) OR "context_entity_relation"."relation_to_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = (V1."id"))))))) )

FROM | TO | THROUGH - result count: 3162 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = (V1."id")))))) OR "context_entity_relation"."relation_to_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = (V1."id")))))) OR "context_entity_relation"."relation_through_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = (V1."id"))))))) )

In [63]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "John" )
test_value_list.append( "Michael" )
test_value_list.append( "Robert" )
test_value_list.append( "Steve" )
test_value_list.append( "Larry" )

trait_name_q = Q( entity_trait__name = "first_name" )
trait_value_q = ~ Q( entity_trait__value__in = test_value_list )
combined_q = trait_name_q & trait_value_q
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 245 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_trait" ON ("context_entity"."id" = "context_entity_trait"."entity_id") WHERE ("context_entity_trait"."name" = first_name AND NOT ("context_entity"."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (John, Michael, Robert, Steve, Larry) AND U1."id" = ("context_entity_trait"."id"))))))
Entity ID: 8 ( context_text-Person-161 )
Entity ID: 9 ( context_text-Person-46 )
Entity ID: 11 ( context_text-Person-164 )
Entity ID: 13 ( context_text-Person-166 )
Entity ID: 14 ( context_text-Person-186 )
Entity ID: 10 ( context_text-Person-872 )
Entity ID: 16 ( context_text-Person-163 )
Entity ID: 15 ( context_text-Person-162 )
Entity ID: 18 ( context_text-Person-30 )
Entity ID: 20 ( context_text-Person-175 )
Entity ID: 22 ( context_text-Person-176 )
Entity ID: 23 ( context_text-Person-173 )
Entity ID: 19 ( context_text-Person-750 )
Entity ID: 24 ( context_text-Person-172 )
Entity ID: 25 ( context_text-Person-32 )
Entity ID: 28 ( context_text-Person-182 )
Entity ID: 29 ( context_text-Person-878 )
Entity ID: 30 ( context_text-Person-180 )
Entity ID: 31 ( context_text-Person-876 )
Entity ID: 32 ( context_text-Person-181 )
Entity ID: 27 ( context_text-Person-877 )
Entity ID: 33 ( context_text-Person-875 )
Entity ID: 35 ( context_text-Person-178 )
Entity ID: 36 ( context_text-Person-179 )
Entity ID: 39 ( context_text-Person-914 )
Entity ID: 40 ( context_text-Person-234 )
Entity ID: 41 ( context_text-Person-909 )
Entity ID: 42 ( context_text-Person-213 )
Entity ID: 43 ( context_text-Person-910 )
Entity ID: 45 ( context_text-Person-912 )
Entity ID: 46 ( context_text-Person-215 )
Entity ID: 47 ( context_text-Person-911 )
Entity ID: 49 ( context_text-Person-73 )
Entity ID: 51 ( context_text-Person-237 )
Entity ID: 52 ( context_text-Person-236 )
Entity ID: 53 ( context_text-Person-239 )
Entity ID: 50 ( context_text-Person-901 )
Entity ID: 55 ( context_text-Person-23 )
Entity ID: 57 ( context_text-Person-933 )
Entity ID: 58 ( context_text-Person-269 )
Entity ID: 60 ( context_text-Person-934 )
Entity ID: 61 ( context_text-Person-1049 )
Entity ID: 59 ( context_text-Person-268 )
Entity ID: 165 ( context_text-Person-190 )
Entity ID: 65 ( context_text-Person-275 )
Entity ID: 66 ( context_text-Person-274 )
Entity ID: 67 ( context_text-Person-273 )
Entity ID: 63 ( context_text-Person-36 )
Entity ID: 64 ( context_text-Person-272 )
Entity ID: 72 ( context_text-Person-102 )
Entity ID: 69 ( context_text-Person-69 )
Entity ID: 71 ( context_text-Person-290 )
Entity ID: 76 ( context_text-Person-292 )
Entity ID: 74 ( context_text-Person-217 )
Entity ID: 77 ( context_text-Person-295 )
Entity ID: 78 ( context_text-Person-752 )
Entity ID: 75 ( context_text-Person-753 )
Entity ID: 82 ( context_text-Person-311 )
Entity ID: 83 ( context_text-Person-310 )
Entity ID: 80 ( context_text-Person-591 )
Entity ID: 87 ( context_text-Person-308 )
Entity ID: 88 ( context_text-Person-1003 )
Entity ID: 89 ( context_text-Person-307 )
Entity ID: 90 ( context_text-Person-1001 )
Entity ID: 92 ( context_text-Person-302 )
Entity ID: 94 ( context_text-Person-1009 )
Entity ID: 93 ( context_text-Person-306 )
Entity ID: 95 ( context_text-Person-304 )
Entity ID: 96 ( context_text-Person-303 )
Entity ID: 97 ( context_text-Person-305 )
Entity ID: 101 ( context_text-Person-263 )
Entity ID: 102 ( context_text-Person-261 )
Entity ID: 103 ( context_text-Person-259 )
Entity ID: 99 ( context_text-Person-66 )
Entity ID: 105 ( context_text-Person-223 )
Entity ID: 107 ( context_text-Person-227 )
Entity ID: 108 ( context_text-Person-229 )
Entity ID: 109 ( context_text-Person-228 )
Entity ID: 111 ( context_text-Person-1029 )
Entity ID: 106 ( context_text-Person-225 )
Entity ID: 112 ( context_text-Person-224 )
Entity ID: 113 ( context_text-Person-230 )
Entity ID: 118 ( context_text-Person-231 )
Entity ID: 119 ( context_text-Person-233 )
Entity ID: 117 ( context_text-Person-232 )
Entity ID: 121 ( context_text-Person-221 )
Entity ID: 122 ( context_text-Person-220 )
Entity ID: 123 ( context_text-Person-219 )
Entity ID: 124 ( context_text-Person-218 )
Entity ID: 127 ( context_text-Person-980 )
Entity ID: 128 ( context_text-Person-1044 )
Entity ID: 129 ( context_text-Person-188 )
Entity ID: 130 ( context_text-Person-187 )
Entity ID: 126 ( context_text-Person-1043 )
Entity ID: 131 ( context_text-Person-189 )
Entity ID: 137 ( context_text-Person-160 )
Entity ID: 138 ( context_text-Person-156 )
Entity ID: 136 ( context_text-Person-159 )
Entity ID: 140 ( context_text-Person-157 )
Entity ID: 142 ( context_text-Person-167 )
Entity ID: 144 ( context_text-Person-168 )
Entity ID: 145 ( context_text-Person-889 )
Entity ID: 143 ( context_text-Person-169 )
Entity ID: 148 ( context_text-Person-171 )
Entity ID: 149 ( context_text-Person-874 )
Entity ID: 151 ( context_text-Person-170 )
Entity ID: 153 ( context_text-Person-143 )
Entity ID: 155 ( context_text-Person-197 )
Entity ID: 156 ( context_text-Person-854 )
Entity ID: 162 ( context_text-Person-184 )
Entity ID: 159 ( context_text-Person-195 )
Entity ID: 154 ( context_text-Person-194 )
Entity ID: 161 ( context_text-Person-183 )
Entity ID: 163 ( context_text-Person-185 )
Entity ID: 158 ( context_text-Person-193 )
Entity ID: 167 ( context_text-Person-751 )
Entity ID: 168 ( context_text-Person-191 )
Entity ID: 166 ( context_text-Person-906 )
Entity ID: 169 ( context_text-Person-907 )
Entity ID: 171 ( context_text-Person-48 )
Entity ID: 173 ( context_text-Person-206 )
Entity ID: 174 ( context_text-Person-207 )
Entity ID: 175 ( context_text-Person-210 )
Entity ID: 176 ( context_text-Person-209 )
Entity ID: 172 ( context_text-Person-204 )
Entity ID: 180 ( context_text-Person-264 )
Entity ID: 181 ( context_text-Person-265 )
Entity ID: 182 ( context_text-Person-924 )
Entity ID: 184 ( context_text-Person-921 )
Entity ID: 185 ( context_text-Person-923 )
Entity ID: 179 ( context_text-Person-925 )
Entity ID: 190 ( context_text-Person-920 )
Entity ID: 189 ( context_text-Person-919 )
Entity ID: 192 ( context_text-Person-90 )
Entity ID: 194 ( context_text-Person-244 )
Entity ID: 195 ( context_text-Person-242 )
Entity ID: 193 ( context_text-Person-938 )
Entity ID: 197 ( context_text-Person-942 )
Entity ID: 198 ( context_text-Person-939 )
Entity ID: 199 ( context_text-Person-941 )
Entity ID: 200 ( context_text-Person-316 )
Entity ID: 201 ( context_text-Person-243 )
Entity ID: 204 ( context_text-Person-248 )
Entity ID: 147 ( context_text-Person-56 )
Entity ID: 196 ( context_text-Person-940 )
Entity ID: 205 ( context_text-Person-926 )
Entity ID: 206 ( context_text-Person-250 )
Entity ID: 207 ( context_text-Person-927 )
Entity ID: 203 ( context_text-Person-255 )
Entity ID: 209 ( context_text-Person-278 )
Entity ID: 211 ( context_text-Person-283 )
Entity ID: 212 ( context_text-Person-287 )
Entity ID: 157 ( context_text-Person-196 )
Entity ID: 213 ( context_text-Person-284 )
Entity ID: 214 ( context_text-Person-282 )
Entity ID: 215 ( context_text-Person-285 )
Entity ID: 216 ( context_text-Person-280 )
Entity ID: 217 ( context_text-Person-279 )
Entity ID: 210 ( context_text-Person-281 )
Entity ID: 220 ( context_text-Person-198 )
Entity ID: 221 ( context_text-Person-288 )
Entity ID: 222 ( context_text-Person-943 )
Entity ID: 226 ( context_text-Person-135 )
Entity ID: 227 ( context_text-Person-314 )
Entity ID: 228 ( context_text-Person-315 )
Entity ID: 224 ( context_text-Person-60 )
Entity ID: 225 ( context_text-Person-972 )
Entity ID: 232 ( context_text-Person-1021 )
Entity ID: 233 ( context_text-Person-301 )
Entity ID: 234 ( context_text-Person-1022 )
Entity ID: 231 ( context_text-Person-1023 )
Entity ID: 236 ( context_text-Person-297 )
Entity ID: 238 ( context_text-Person-1018 )
Entity ID: 239 ( context_text-Person-300 )
Entity ID: 240 ( context_text-Person-298 )
Entity ID: 241 ( context_text-Person-1017 )
Entity ID: 242 ( context_text-Person-1019 )
Entity ID: 243 ( context_text-Person-299 )
Entity ID: 246 ( context_text-Person-1016 )
Entity ID: 247 ( context_text-Person-277 )
Entity ID: 248 ( context_text-Person-905 )
Entity ID: 245 ( context_text-Person-1014 )
Entity ID: 249 ( context_text-Person-1010 )
Entity ID: 250 ( context_text-Person-1011 )
Entity ID: 251 ( context_text-Person-1013 )
Entity ID: 252 ( context_text-Person-763 )
Entity ID: 253 ( context_text-Person-438 )
Entity ID: 254 ( context_text-Person-1012 )
Entity ID: 255 ( context_text-Person-1015 )
Entity ID: 256 ( context_text-Person-276 )
Entity ID: 258 ( context_text-Person-293 )
Entity ID: 260 ( context_text-Person-296 )
Entity ID: 259 ( context_text-Person-294 )
Entity ID: 262 ( context_text-Person-117 )
Entity ID: 263 ( context_text-Person-267 )
Entity ID: 266 ( context_text-Person-235 )
Entity ID: 267 ( context_text-Person-241 )
Entity ID: 268 ( context_text-Person-240 )
Entity ID: 269 ( context_text-Person-238 )
Entity ID: 135 ( context_text-Person-155 )
Entity ID: 273 ( context_text-Person-1028 )
Entity ID: 274 ( context_text-Person-1024 )
Entity ID: 276 ( context_text-Person-317 )
Entity ID: 277 ( context_text-Person-251 )
Entity ID: 278 ( context_text-Person-1027 )
Entity ID: 279 ( context_text-Person-755 )
Entity ID: 280 ( context_text-Person-253 )
Entity ID: 281 ( context_text-Person-247 )
Entity ID: 282 ( context_text-Person-256 )
Entity ID: 275 ( context_text-Person-249 )
Entity ID: 283 ( context_text-Person-245 )
Entity ID: 284 ( context_text-Person-254 )
Entity ID: 285 ( context_text-Person-246 )
Entity ID: 271 ( context_text-Person-1026 )
Entity ID: 287 ( context_text-Person-257 )
Entity ID: 289 ( context_text-Person-1033 )
Entity ID: 290 ( context_text-Person-1037 )
Entity ID: 291 ( context_text-Person-148 )
Entity ID: 292 ( context_text-Person-1030 )
Entity ID: 293 ( context_text-Person-1031 )
Entity ID: 294 ( context_text-Person-1032 )
Entity ID: 295 ( context_text-Person-1036 )
Entity ID: 296 ( context_text-Person-1035 )
Entity ID: 288 ( context_text-Person-1034 )
Entity ID: 298 ( context_text-Person-592 )
Entity ID: 301 ( context_text-Person-1045 )
Entity ID: 299 ( context_text-Person-1046 )
Entity ID: 300 ( context_text-Person-756 )
Entity ID: 315 ( context_text-Person-202 )
Entity ID: 303 ( context_text-Person-211 )
Entity ID: 305 ( context_text-Person-216 )
Entity ID: 306 ( context_text-Person-212 )
Entity ID: 304 ( context_text-Person-214 )
Entity ID: 308 ( context_text-Person-1041 )
Entity ID: 311 ( context_text-Person-1042 )
Entity ID: 316 ( context_text-Person-201 )
Entity ID: 317 ( context_text-Person-200 )
Entity ID: 318 ( context_text-Person-199 )
Entity ID: 319 ( context_text-Person-205 )
Entity ID: 86 ( context_text-Person-1002 )
Entity ID: 100 ( context_text-Person-754 )
Entity ID: 313 ( context_text-Person-1038 )
Entity ID: 314 ( context_text-Person-203 )
Entity ID: 38 ( context_text-Person-913 )
Entity ID: 56 ( context_text-Person-935 )

In [64]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 2177 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (John, Michael, Robert, Steve, Larry) AND U1."id" = (V1."id")))))) )

TO - result count: 2944 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (John, Michael, Robert, Steve, Larry) AND U1."id" = (V1."id")))))) )

THROUGH - result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (John, Michael, Robert, Steve, Larry) AND U1."id" = (V1."id")))))) )

FROM | TO - result count: 3094 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (John, Michael, Robert, Steve, Larry) AND U1."id" = (V1."id")))))) OR "context_entity_relation"."relation_to_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (John, Michael, Robert, Steve, Larry) AND U1."id" = (V1."id"))))))) )

FROM | TO | THROUGH - result count: 3094 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (John, Michael, Robert, Steve, Larry) AND U1."id" = (V1."id")))))) OR "context_entity_relation"."relation_to_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (John, Michael, Robert, Steve, Larry) AND U1."id" = (V1."id")))))) OR "context_entity_relation"."relation_through_id" IN (SELECT V0."id" FROM "context_entity" V0 INNER JOIN "context_entity_trait" V1 ON (V0."id" = V1."entity_id") WHERE (V1."name" = first_name AND NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_trait" U1 WHERE (U1."value" IN (John, Michael, Robert, Steve, Larry) AND U1."id" = (V1."id"))))))) )

test IN RANGE for article pub_date values


In [74]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

trait_name_q = Q( entity_trait__name = "pub_date" )
left_range_q = Q( entity_trait__value__gte = "2010-02-08" )
right_range_q = Q( entity_trait__value__lte = "2010-02-13" )
combined_q = trait_name_q & left_range_q & right_range_q

entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 18 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_trait" ON ("context_entity"."id" = "context_entity_trait"."entity_id") WHERE ("context_entity_trait"."name" = pub_date AND "context_entity_trait"."value" >= 2010-02-08 AND "context_entity_trait"."value" <= 2010-02-13))
Entity ID: 312 ( context_text-Article-94442 )
Entity ID: 98 ( context_text-Article-28499 )
Entity ID: 104 ( context_text-Article-28598 )
Entity ID: 114 ( context_text-Article-28610 )
Entity ID: 116 ( context_text-Article-28649 )
Entity ID: 120 ( context_text-Article-28741 )
Entity ID: 125 ( context_text-Article-28846 )
Entity ID: 230 ( context_text-Article-94104 )
Entity ID: 235 ( context_text-Article-94110 )
Entity ID: 244 ( context_text-Article-94125 )
Entity ID: 257 ( context_text-Article-94128 )
Entity ID: 261 ( context_text-Article-94140 )
Entity ID: 265 ( context_text-Article-94301 )
Entity ID: 270 ( context_text-Article-94311 )
Entity ID: 286 ( context_text-Article-94326 )
Entity ID: 297 ( context_text-Article-94405 )
Entity ID: 302 ( context_text-Article-94417 )
Entity ID: 307 ( context_text-Article-94430 )

In [75]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 170 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = pub_date AND U1."value" >= 2010-02-08 AND U1."value" <= 2010-02-13)) )

TO - result count: 18 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = pub_date AND U1."value" >= 2010-02-08 AND U1."value" <= 2010-02-13)) )

THROUGH - result count: 1255 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = pub_date AND U1."value" >= 2010-02-08 AND U1."value" <= 2010-02-13)) )

FROM | TO - result count: 188 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = pub_date AND U1."value" >= 2010-02-08 AND U1."value" <= 2010-02-13)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = pub_date AND U1."value" >= 2010-02-08 AND U1."value" <= 2010-02-13))) )

FROM | TO | THROUGH - result count: 1443 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = pub_date AND U1."value" >= 2010-02-08 AND U1."value" <= 2010-02-13)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = pub_date AND U1."value" >= 2010-02-08 AND U1."value" <= 2010-02-13)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = pub_date AND U1."value" >= 2010-02-08 AND U1."value" <= 2010-02-13))) )

filter on entity type slugs

Figure out which relation roles we are focusing on (combination of FROM, TO, THROUGH), then build out query to properly filter.

filter just entities based on entity type slugs


In [28]:
# try to just find the entity with the desired identifier.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
entity_qs = entity_qs.filter( entity_types__entity_type__slug = "person" )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 268 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_types" ON ("context_entity"."id" = "context_entity_types"."entity_id") INNER JOIN "context_entity_type" ON ("context_entity_types"."entity_type_id" = "context_entity_type"."id") WHERE "context_entity_type"."slug" = person)

In [29]:
# try to just find the entity with the desired identifier.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
combined_q = Q( entity_types__entity_type__slug = "person" )
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 268 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_types" ON ("context_entity"."id" = "context_entity_types"."entity_id") INNER JOIN "context_entity_type" ON ("context_entity_types"."entity_type_id" = "context_entity_type"."id") WHERE "context_entity_type"."slug" = person)

filter relations based on entity type slugs


In [31]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 2320 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) )

TO - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) )

THROUGH - result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) )

FROM | TO - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person)) )

FROM | TO | THROUGH - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person)) )

test IN list of entity types


In [82]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "person" )
test_value_list.append( "article" )

combined_q = Q( entity_types__entity_type__slug__in = test_value_list )
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 315 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_types" ON ("context_entity"."id" = "context_entity_types"."entity_id") INNER JOIN "context_entity_type" ON ("context_entity_types"."entity_type_id" = "context_entity_type"."id") WHERE "context_entity_type"."slug" IN (person, article))
Entity ID: 1 ( test_entity_1 )
Entity ID: 2 ( test_entity_2 )
Entity ID: 3 ( test_entity_3 )
Entity ID: 4 ( test_entity_4 )
Entity ID: 5 ( context_text-Article-21409 )
Entity ID: 8 ( context_text-Person-161 )
Entity ID: 9 ( context_text-Person-46 )
Entity ID: 10 ( context_text-Person-872 )
Entity ID: 11 ( context_text-Person-164 )
Entity ID: 12 ( context_text-Person-165 )
Entity ID: 13 ( context_text-Person-166 )
Entity ID: 14 ( context_text-Person-186 )
Entity ID: 15 ( context_text-Person-162 )
Entity ID: 16 ( context_text-Person-163 )
Entity ID: 17 ( context_text-Article-21483 )
Entity ID: 18 ( context_text-Person-30 )
Entity ID: 19 ( context_text-Person-750 )
Entity ID: 20 ( context_text-Person-175 )
Entity ID: 21 ( context_text-Person-174 )
Entity ID: 22 ( context_text-Person-176 )
Entity ID: 23 ( context_text-Person-173 )
Entity ID: 24 ( context_text-Person-172 )
Entity ID: 25 ( context_text-Person-32 )
Entity ID: 26 ( context_text-Article-21509 )
Entity ID: 27 ( context_text-Person-877 )
Entity ID: 28 ( context_text-Person-182 )
Entity ID: 29 ( context_text-Person-878 )
Entity ID: 30 ( context_text-Person-180 )
Entity ID: 31 ( context_text-Person-876 )
Entity ID: 32 ( context_text-Person-181 )
Entity ID: 33 ( context_text-Person-875 )
Entity ID: 34 ( context_text-Article-21512 )
Entity ID: 35 ( context_text-Person-178 )
Entity ID: 36 ( context_text-Person-179 )
Entity ID: 37 ( context_text-Article-21627 )
Entity ID: 38 ( context_text-Person-913 )
Entity ID: 39 ( context_text-Person-914 )
Entity ID: 40 ( context_text-Person-234 )
Entity ID: 41 ( context_text-Person-909 )
Entity ID: 42 ( context_text-Person-213 )
Entity ID: 43 ( context_text-Person-910 )
Entity ID: 44 ( context_text-Person-132 )
Entity ID: 45 ( context_text-Person-912 )
Entity ID: 46 ( context_text-Person-215 )
Entity ID: 47 ( context_text-Person-911 )
Entity ID: 48 ( context_text-Article-21661 )
Entity ID: 49 ( context_text-Person-73 )
Entity ID: 50 ( context_text-Person-901 )
Entity ID: 51 ( context_text-Person-237 )
Entity ID: 52 ( context_text-Person-236 )
Entity ID: 53 ( context_text-Person-239 )
Entity ID: 54 ( context_text-Article-21719 )
Entity ID: 55 ( context_text-Person-23 )
Entity ID: 56 ( context_text-Person-935 )
Entity ID: 57 ( context_text-Person-933 )
Entity ID: 58 ( context_text-Person-269 )
Entity ID: 59 ( context_text-Person-268 )
Entity ID: 60 ( context_text-Person-934 )
Entity ID: 61 ( context_text-Person-1049 )
Entity ID: 62 ( context_text-Article-21738 )
Entity ID: 63 ( context_text-Person-36 )
Entity ID: 64 ( context_text-Person-272 )
Entity ID: 65 ( context_text-Person-275 )
Entity ID: 66 ( context_text-Person-274 )
Entity ID: 67 ( context_text-Person-273 )
Entity ID: 68 ( context_text-Article-21790 )
Entity ID: 69 ( context_text-Person-69 )
Entity ID: 70 ( context_text-Person-289 )
Entity ID: 71 ( context_text-Person-290 )
Entity ID: 72 ( context_text-Person-102 )
Entity ID: 73 ( context_text-Article-21827 )
Entity ID: 74 ( context_text-Person-217 )
Entity ID: 75 ( context_text-Person-753 )
Entity ID: 76 ( context_text-Person-292 )
Entity ID: 77 ( context_text-Person-295 )
Entity ID: 78 ( context_text-Person-752 )
Entity ID: 79 ( context_text-Article-21890 )
Entity ID: 80 ( context_text-Person-591 )
Entity ID: 81 ( context_text-Person-312 )
Entity ID: 82 ( context_text-Person-311 )
Entity ID: 83 ( context_text-Person-310 )
Entity ID: 84 ( context_text-Article-21925 )
Entity ID: 85 ( context_text-Person-84 )
Entity ID: 86 ( context_text-Person-1002 )
Entity ID: 87 ( context_text-Person-308 )
Entity ID: 88 ( context_text-Person-1003 )
Entity ID: 89 ( context_text-Person-307 )
Entity ID: 90 ( context_text-Person-1001 )
Entity ID: 91 ( context_text-Article-28274 )
Entity ID: 92 ( context_text-Person-302 )
Entity ID: 93 ( context_text-Person-306 )
Entity ID: 94 ( context_text-Person-1009 )
Entity ID: 95 ( context_text-Person-304 )
Entity ID: 96 ( context_text-Person-303 )
Entity ID: 97 ( context_text-Person-305 )
Entity ID: 98 ( context_text-Article-28499 )
Entity ID: 99 ( context_text-Person-66 )
Entity ID: 100 ( context_text-Person-754 )
Entity ID: 101 ( context_text-Person-263 )
Entity ID: 102 ( context_text-Person-261 )
Entity ID: 103 ( context_text-Person-259 )
Entity ID: 104 ( context_text-Article-28598 )
Entity ID: 105 ( context_text-Person-223 )
Entity ID: 106 ( context_text-Person-225 )
Entity ID: 107 ( context_text-Person-227 )
Entity ID: 108 ( context_text-Person-229 )
Entity ID: 109 ( context_text-Person-228 )
Entity ID: 110 ( context_text-Person-226 )
Entity ID: 111 ( context_text-Person-1029 )
Entity ID: 112 ( context_text-Person-224 )
Entity ID: 113 ( context_text-Person-230 )
Entity ID: 114 ( context_text-Article-28610 )
Entity ID: 115 ( context_text-Person-222 )
Entity ID: 116 ( context_text-Article-28649 )
Entity ID: 117 ( context_text-Person-232 )
Entity ID: 118 ( context_text-Person-231 )
Entity ID: 119 ( context_text-Person-233 )
Entity ID: 120 ( context_text-Article-28741 )
Entity ID: 121 ( context_text-Person-221 )
Entity ID: 122 ( context_text-Person-220 )
Entity ID: 123 ( context_text-Person-219 )
Entity ID: 124 ( context_text-Person-218 )
Entity ID: 125 ( context_text-Article-28846 )
Entity ID: 126 ( context_text-Person-1043 )
Entity ID: 127 ( context_text-Person-980 )
Entity ID: 128 ( context_text-Person-1044 )
Entity ID: 129 ( context_text-Person-188 )
Entity ID: 130 ( context_text-Person-187 )
Entity ID: 131 ( context_text-Person-189 )
Entity ID: 132 ( context_text-Article-90948 )
Entity ID: 135 ( context_text-Person-155 )
Entity ID: 136 ( context_text-Person-159 )
Entity ID: 137 ( context_text-Person-160 )
Entity ID: 138 ( context_text-Person-156 )
Entity ID: 139 ( context_text-Person-158 )
Entity ID: 140 ( context_text-Person-157 )
Entity ID: 141 ( context_text-Article-90983 )
Entity ID: 142 ( context_text-Person-167 )
Entity ID: 143 ( context_text-Person-169 )
Entity ID: 144 ( context_text-Person-168 )
Entity ID: 145 ( context_text-Person-889 )
Entity ID: 146 ( context_text-Article-91000 )
Entity ID: 147 ( context_text-Person-56 )
Entity ID: 148 ( context_text-Person-171 )
Entity ID: 149 ( context_text-Person-874 )
Entity ID: 150 ( context_text-Person-873 )
Entity ID: 151 ( context_text-Person-170 )
Entity ID: 152 ( context_text-Article-91036 )
Entity ID: 153 ( context_text-Person-143 )
Entity ID: 154 ( context_text-Person-194 )
Entity ID: 155 ( context_text-Person-197 )
Entity ID: 156 ( context_text-Person-854 )
Entity ID: 157 ( context_text-Person-196 )
Entity ID: 158 ( context_text-Person-193 )
Entity ID: 159 ( context_text-Person-195 )
Entity ID: 160 ( context_text-Article-91038 )
Entity ID: 161 ( context_text-Person-183 )
Entity ID: 162 ( context_text-Person-184 )
Entity ID: 163 ( context_text-Person-185 )
Entity ID: 164 ( context_text-Article-91068 )
Entity ID: 165 ( context_text-Person-190 )
Entity ID: 166 ( context_text-Person-906 )
Entity ID: 167 ( context_text-Person-751 )
Entity ID: 168 ( context_text-Person-191 )
Entity ID: 169 ( context_text-Person-907 )
Entity ID: 170 ( context_text-Article-91071 )
Entity ID: 171 ( context_text-Person-48 )
Entity ID: 172 ( context_text-Person-204 )
Entity ID: 173 ( context_text-Person-206 )
Entity ID: 174 ( context_text-Person-207 )
Entity ID: 175 ( context_text-Person-210 )
Entity ID: 176 ( context_text-Person-209 )
Entity ID: 177 ( context_text-Article-91112 )
Entity ID: 178 ( context_text-Person-262 )
Entity ID: 179 ( context_text-Person-925 )
Entity ID: 180 ( context_text-Person-264 )
Entity ID: 181 ( context_text-Person-265 )
Entity ID: 182 ( context_text-Person-924 )
Entity ID: 183 ( context_text-Person-922 )
Entity ID: 184 ( context_text-Person-921 )
Entity ID: 185 ( context_text-Person-923 )
Entity ID: 186 ( context_text-Article-91114 )
Entity ID: 187 ( context_text-Article-91132 )
Entity ID: 188 ( context_text-Person-260 )
Entity ID: 189 ( context_text-Person-919 )
Entity ID: 190 ( context_text-Person-920 )
Entity ID: 191 ( context_text-Article-91133 )
Entity ID: 192 ( context_text-Person-90 )
Entity ID: 193 ( context_text-Person-938 )
Entity ID: 194 ( context_text-Person-244 )
Entity ID: 195 ( context_text-Person-242 )
Entity ID: 196 ( context_text-Person-940 )
Entity ID: 197 ( context_text-Person-942 )
Entity ID: 198 ( context_text-Person-939 )
Entity ID: 199 ( context_text-Person-941 )
Entity ID: 200 ( context_text-Person-316 )
Entity ID: 201 ( context_text-Person-243 )
Entity ID: 202 ( context_text-Article-91157 )
Entity ID: 203 ( context_text-Person-255 )
Entity ID: 204 ( context_text-Person-248 )
Entity ID: 205 ( context_text-Person-926 )
Entity ID: 206 ( context_text-Person-250 )
Entity ID: 207 ( context_text-Person-927 )
Entity ID: 208 ( context_text-Article-91197 )
Entity ID: 209 ( context_text-Person-278 )
Entity ID: 210 ( context_text-Person-281 )
Entity ID: 211 ( context_text-Person-283 )
Entity ID: 212 ( context_text-Person-287 )
Entity ID: 213 ( context_text-Person-284 )
Entity ID: 214 ( context_text-Person-282 )
Entity ID: 215 ( context_text-Person-285 )
Entity ID: 216 ( context_text-Person-280 )
Entity ID: 217 ( context_text-Person-279 )
Entity ID: 218 ( context_text-Person-286 )
Entity ID: 219 ( context_text-Article-91199 )
Entity ID: 220 ( context_text-Person-198 )
Entity ID: 221 ( context_text-Person-288 )
Entity ID: 222 ( context_text-Person-943 )
Entity ID: 223 ( context_text-Article-91254 )
Entity ID: 224 ( context_text-Person-60 )
Entity ID: 225 ( context_text-Person-972 )
Entity ID: 226 ( context_text-Person-135 )
Entity ID: 227 ( context_text-Person-314 )
Entity ID: 228 ( context_text-Person-315 )
Entity ID: 229 ( context_text-Person-313 )
Entity ID: 230 ( context_text-Article-94104 )
Entity ID: 231 ( context_text-Person-1023 )
Entity ID: 232 ( context_text-Person-1021 )
Entity ID: 233 ( context_text-Person-301 )
Entity ID: 234 ( context_text-Person-1022 )
Entity ID: 235 ( context_text-Article-94110 )
Entity ID: 236 ( context_text-Person-297 )
Entity ID: 237 ( context_text-Person-1020 )
Entity ID: 238 ( context_text-Person-1018 )
Entity ID: 239 ( context_text-Person-300 )
Entity ID: 240 ( context_text-Person-298 )
Entity ID: 241 ( context_text-Person-1017 )
Entity ID: 242 ( context_text-Person-1019 )
Entity ID: 243 ( context_text-Person-299 )
Entity ID: 244 ( context_text-Article-94125 )
Entity ID: 245 ( context_text-Person-1014 )
Entity ID: 246 ( context_text-Person-1016 )
Entity ID: 247 ( context_text-Person-277 )
Entity ID: 248 ( context_text-Person-905 )
Entity ID: 249 ( context_text-Person-1010 )
Entity ID: 250 ( context_text-Person-1011 )
Entity ID: 251 ( context_text-Person-1013 )
Entity ID: 252 ( context_text-Person-763 )
Entity ID: 253 ( context_text-Person-438 )
Entity ID: 254 ( context_text-Person-1012 )
Entity ID: 255 ( context_text-Person-1015 )
Entity ID: 256 ( context_text-Person-276 )
Entity ID: 257 ( context_text-Article-94128 )
Entity ID: 258 ( context_text-Person-293 )
Entity ID: 259 ( context_text-Person-294 )
Entity ID: 260 ( context_text-Person-296 )
Entity ID: 261 ( context_text-Article-94140 )
Entity ID: 262 ( context_text-Person-117 )
Entity ID: 263 ( context_text-Person-267 )
Entity ID: 264 ( context_text-Person-266 )
Entity ID: 265 ( context_text-Article-94301 )
Entity ID: 266 ( context_text-Person-235 )
Entity ID: 267 ( context_text-Person-241 )
Entity ID: 268 ( context_text-Person-240 )
Entity ID: 269 ( context_text-Person-238 )
Entity ID: 270 ( context_text-Article-94311 )
Entity ID: 271 ( context_text-Person-1026 )
Entity ID: 272 ( context_text-Person-1025 )
Entity ID: 273 ( context_text-Person-1028 )
Entity ID: 274 ( context_text-Person-1024 )
Entity ID: 275 ( context_text-Person-249 )
Entity ID: 276 ( context_text-Person-317 )
Entity ID: 277 ( context_text-Person-251 )
Entity ID: 278 ( context_text-Person-1027 )
Entity ID: 279 ( context_text-Person-755 )
Entity ID: 280 ( context_text-Person-253 )
Entity ID: 281 ( context_text-Person-247 )
Entity ID: 282 ( context_text-Person-256 )
Entity ID: 283 ( context_text-Person-245 )
Entity ID: 284 ( context_text-Person-254 )
Entity ID: 285 ( context_text-Person-246 )
Entity ID: 286 ( context_text-Article-94326 )
Entity ID: 287 ( context_text-Person-257 )
Entity ID: 288 ( context_text-Person-1034 )
Entity ID: 289 ( context_text-Person-1033 )
Entity ID: 290 ( context_text-Person-1037 )
Entity ID: 291 ( context_text-Person-148 )
Entity ID: 292 ( context_text-Person-1030 )
Entity ID: 293 ( context_text-Person-1031 )
Entity ID: 294 ( context_text-Person-1032 )
Entity ID: 295 ( context_text-Person-1036 )
Entity ID: 296 ( context_text-Person-1035 )
Entity ID: 297 ( context_text-Article-94405 )
Entity ID: 298 ( context_text-Person-592 )
Entity ID: 299 ( context_text-Person-1046 )
Entity ID: 300 ( context_text-Person-756 )
Entity ID: 301 ( context_text-Person-1045 )
Entity ID: 302 ( context_text-Article-94417 )
Entity ID: 303 ( context_text-Person-211 )
Entity ID: 304 ( context_text-Person-214 )
Entity ID: 305 ( context_text-Person-216 )
Entity ID: 306 ( context_text-Person-212 )
Entity ID: 307 ( context_text-Article-94430 )
Entity ID: 308 ( context_text-Person-1041 )
Entity ID: 309 ( context_text-Person-1039 )
Entity ID: 310 ( context_text-Person-1040 )
Entity ID: 311 ( context_text-Person-1042 )
Entity ID: 312 ( context_text-Article-94442 )
Entity ID: 313 ( context_text-Person-1038 )
Entity ID: 314 ( context_text-Person-203 )
Entity ID: 315 ( context_text-Person-202 )
Entity ID: 316 ( context_text-Person-201 )
Entity ID: 317 ( context_text-Person-200 )
Entity ID: 318 ( context_text-Person-199 )
Entity ID: 319 ( context_text-Person-205 )

In [83]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | THROUGH
combined_q = from_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO | THROUGH
combined_q = to_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nTO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = from_q | to_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 2746 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) )

TO - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) )

THROUGH - result count: 2743 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) )

FROM | TO - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article))) )

FROM | THROUGH - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article))) )

TO | THROUGH - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article))) )

FROM | TO | THROUGH - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article))) )

test NOT IN list of entity types


In [39]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "peregrine" )
test_value_list.append( "chartreuse" )
test_value_list.append( "bumblebee" )

combined_q = ~ Q( entity_types__entity_type__slug__in = test_value_list )
entity_qs = entity_qs.filter( combined_q )

print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" WHERE NOT ("context_entity"."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee))))
Entity ID: 1 ( test_entity_1 )
Entity ID: 2 ( test_entity_2 )
Entity ID: 3 ( test_entity_3 )
Entity ID: 4 ( test_entity_4 )
Entity ID: 5 ( context_text-Article-21409 )
Entity ID: 6 ( context_text-Newspaper-1 )
Entity ID: 7 ( context_text-Organization-1 )
Entity ID: 8 ( context_text-Person-161 )
Entity ID: 9 ( context_text-Person-46 )
Entity ID: 10 ( context_text-Person-872 )
Entity ID: 11 ( context_text-Person-164 )
Entity ID: 12 ( context_text-Person-165 )
Entity ID: 13 ( context_text-Person-166 )
Entity ID: 14 ( context_text-Person-186 )
Entity ID: 15 ( context_text-Person-162 )
Entity ID: 16 ( context_text-Person-163 )
Entity ID: 17 ( context_text-Article-21483 )
Entity ID: 18 ( context_text-Person-30 )
Entity ID: 19 ( context_text-Person-750 )
Entity ID: 20 ( context_text-Person-175 )
Entity ID: 21 ( context_text-Person-174 )
Entity ID: 22 ( context_text-Person-176 )
Entity ID: 23 ( context_text-Person-173 )
Entity ID: 24 ( context_text-Person-172 )
Entity ID: 25 ( context_text-Person-32 )
Entity ID: 26 ( context_text-Article-21509 )
Entity ID: 27 ( context_text-Person-877 )
Entity ID: 28 ( context_text-Person-182 )
Entity ID: 29 ( context_text-Person-878 )
Entity ID: 30 ( context_text-Person-180 )
Entity ID: 31 ( context_text-Person-876 )
Entity ID: 32 ( context_text-Person-181 )
Entity ID: 33 ( context_text-Person-875 )
Entity ID: 34 ( context_text-Article-21512 )
Entity ID: 35 ( context_text-Person-178 )
Entity ID: 36 ( context_text-Person-179 )
Entity ID: 37 ( context_text-Article-21627 )
Entity ID: 38 ( context_text-Person-913 )
Entity ID: 39 ( context_text-Person-914 )
Entity ID: 40 ( context_text-Person-234 )
Entity ID: 41 ( context_text-Person-909 )
Entity ID: 42 ( context_text-Person-213 )
Entity ID: 43 ( context_text-Person-910 )
Entity ID: 44 ( context_text-Person-132 )
Entity ID: 45 ( context_text-Person-912 )
Entity ID: 46 ( context_text-Person-215 )
Entity ID: 47 ( context_text-Person-911 )
Entity ID: 48 ( context_text-Article-21661 )
Entity ID: 49 ( context_text-Person-73 )
Entity ID: 50 ( context_text-Person-901 )
Entity ID: 51 ( context_text-Person-237 )
Entity ID: 52 ( context_text-Person-236 )
Entity ID: 53 ( context_text-Person-239 )
Entity ID: 54 ( context_text-Article-21719 )
Entity ID: 55 ( context_text-Person-23 )
Entity ID: 56 ( context_text-Person-935 )
Entity ID: 57 ( context_text-Person-933 )
Entity ID: 58 ( context_text-Person-269 )
Entity ID: 59 ( context_text-Person-268 )
Entity ID: 60 ( context_text-Person-934 )
Entity ID: 61 ( context_text-Person-1049 )
Entity ID: 171 ( context_text-Person-48 )
Entity ID: 62 ( context_text-Article-21738 )
Entity ID: 63 ( context_text-Person-36 )
Entity ID: 64 ( context_text-Person-272 )
Entity ID: 65 ( context_text-Person-275 )
Entity ID: 66 ( context_text-Person-274 )
Entity ID: 67 ( context_text-Person-273 )
Entity ID: 68 ( context_text-Article-21790 )
Entity ID: 69 ( context_text-Person-69 )
Entity ID: 70 ( context_text-Person-289 )
Entity ID: 71 ( context_text-Person-290 )
Entity ID: 72 ( context_text-Person-102 )
Entity ID: 73 ( context_text-Article-21827 )
Entity ID: 74 ( context_text-Person-217 )
Entity ID: 75 ( context_text-Person-753 )
Entity ID: 76 ( context_text-Person-292 )
Entity ID: 77 ( context_text-Person-295 )
Entity ID: 78 ( context_text-Person-752 )
Entity ID: 79 ( context_text-Article-21890 )
Entity ID: 80 ( context_text-Person-591 )
Entity ID: 81 ( context_text-Person-312 )
Entity ID: 82 ( context_text-Person-311 )
Entity ID: 83 ( context_text-Person-310 )
Entity ID: 84 ( context_text-Article-21925 )
Entity ID: 85 ( context_text-Person-84 )
Entity ID: 86 ( context_text-Person-1002 )
Entity ID: 87 ( context_text-Person-308 )
Entity ID: 88 ( context_text-Person-1003 )
Entity ID: 89 ( context_text-Person-307 )
Entity ID: 90 ( context_text-Person-1001 )
Entity ID: 91 ( context_text-Article-28274 )
Entity ID: 92 ( context_text-Person-302 )
Entity ID: 93 ( context_text-Person-306 )
Entity ID: 94 ( context_text-Person-1009 )
Entity ID: 95 ( context_text-Person-304 )
Entity ID: 96 ( context_text-Person-303 )
Entity ID: 97 ( context_text-Person-305 )
Entity ID: 98 ( context_text-Article-28499 )
Entity ID: 99 ( context_text-Person-66 )
Entity ID: 100 ( context_text-Person-754 )
Entity ID: 101 ( context_text-Person-263 )
Entity ID: 102 ( context_text-Person-261 )
Entity ID: 103 ( context_text-Person-259 )
Entity ID: 104 ( context_text-Article-28598 )
Entity ID: 105 ( context_text-Person-223 )
Entity ID: 106 ( context_text-Person-225 )
Entity ID: 107 ( context_text-Person-227 )
Entity ID: 108 ( context_text-Person-229 )
Entity ID: 109 ( context_text-Person-228 )
Entity ID: 110 ( context_text-Person-226 )
Entity ID: 111 ( context_text-Person-1029 )
Entity ID: 112 ( context_text-Person-224 )
Entity ID: 113 ( context_text-Person-230 )
Entity ID: 114 ( context_text-Article-28610 )
Entity ID: 115 ( context_text-Person-222 )
Entity ID: 116 ( context_text-Article-28649 )
Entity ID: 117 ( context_text-Person-232 )
Entity ID: 118 ( context_text-Person-231 )
Entity ID: 119 ( context_text-Person-233 )
Entity ID: 120 ( context_text-Article-28741 )
Entity ID: 121 ( context_text-Person-221 )
Entity ID: 122 ( context_text-Person-220 )
Entity ID: 123 ( context_text-Person-219 )
Entity ID: 124 ( context_text-Person-218 )
Entity ID: 125 ( context_text-Article-28846 )
Entity ID: 126 ( context_text-Person-1043 )
Entity ID: 127 ( context_text-Person-980 )
Entity ID: 128 ( context_text-Person-1044 )
Entity ID: 129 ( context_text-Person-188 )
Entity ID: 130 ( context_text-Person-187 )
Entity ID: 131 ( context_text-Person-189 )
Entity ID: 132 ( context_text-Article-90948 )
Entity ID: 133 ( context_text-Newspaper-2 )
Entity ID: 134 ( context_text-Organization-4 )
Entity ID: 135 ( context_text-Person-155 )
Entity ID: 136 ( context_text-Person-159 )
Entity ID: 137 ( context_text-Person-160 )
Entity ID: 138 ( context_text-Person-156 )
Entity ID: 139 ( context_text-Person-158 )
Entity ID: 140 ( context_text-Person-157 )
Entity ID: 141 ( context_text-Article-90983 )
Entity ID: 142 ( context_text-Person-167 )
Entity ID: 143 ( context_text-Person-169 )
Entity ID: 144 ( context_text-Person-168 )
Entity ID: 145 ( context_text-Person-889 )
Entity ID: 146 ( context_text-Article-91000 )
Entity ID: 147 ( context_text-Person-56 )
Entity ID: 148 ( context_text-Person-171 )
Entity ID: 149 ( context_text-Person-874 )
Entity ID: 150 ( context_text-Person-873 )
Entity ID: 151 ( context_text-Person-170 )
Entity ID: 152 ( context_text-Article-91036 )
Entity ID: 153 ( context_text-Person-143 )
Entity ID: 154 ( context_text-Person-194 )
Entity ID: 155 ( context_text-Person-197 )
Entity ID: 156 ( context_text-Person-854 )
Entity ID: 157 ( context_text-Person-196 )
Entity ID: 158 ( context_text-Person-193 )
Entity ID: 159 ( context_text-Person-195 )
Entity ID: 160 ( context_text-Article-91038 )
Entity ID: 161 ( context_text-Person-183 )
Entity ID: 162 ( context_text-Person-184 )
Entity ID: 163 ( context_text-Person-185 )
Entity ID: 164 ( context_text-Article-91068 )
Entity ID: 165 ( context_text-Person-190 )
Entity ID: 166 ( context_text-Person-906 )
Entity ID: 167 ( context_text-Person-751 )
Entity ID: 168 ( context_text-Person-191 )
Entity ID: 169 ( context_text-Person-907 )
Entity ID: 170 ( context_text-Article-91071 )
Entity ID: 172 ( context_text-Person-204 )
Entity ID: 173 ( context_text-Person-206 )
Entity ID: 174 ( context_text-Person-207 )
Entity ID: 175 ( context_text-Person-210 )
Entity ID: 176 ( context_text-Person-209 )
Entity ID: 177 ( context_text-Article-91112 )
Entity ID: 178 ( context_text-Person-262 )
Entity ID: 179 ( context_text-Person-925 )
Entity ID: 180 ( context_text-Person-264 )
Entity ID: 181 ( context_text-Person-265 )
Entity ID: 182 ( context_text-Person-924 )
Entity ID: 183 ( context_text-Person-922 )
Entity ID: 184 ( context_text-Person-921 )
Entity ID: 185 ( context_text-Person-923 )
Entity ID: 186 ( context_text-Article-91114 )
Entity ID: 187 ( context_text-Article-91132 )
Entity ID: 188 ( context_text-Person-260 )
Entity ID: 189 ( context_text-Person-919 )
Entity ID: 190 ( context_text-Person-920 )
Entity ID: 191 ( context_text-Article-91133 )
Entity ID: 192 ( context_text-Person-90 )
Entity ID: 193 ( context_text-Person-938 )
Entity ID: 194 ( context_text-Person-244 )
Entity ID: 195 ( context_text-Person-242 )
Entity ID: 196 ( context_text-Person-940 )
Entity ID: 197 ( context_text-Person-942 )
Entity ID: 198 ( context_text-Person-939 )
Entity ID: 199 ( context_text-Person-941 )
Entity ID: 200 ( context_text-Person-316 )
Entity ID: 201 ( context_text-Person-243 )
Entity ID: 202 ( context_text-Article-91157 )
Entity ID: 203 ( context_text-Person-255 )
Entity ID: 204 ( context_text-Person-248 )
Entity ID: 205 ( context_text-Person-926 )
Entity ID: 206 ( context_text-Person-250 )
Entity ID: 207 ( context_text-Person-927 )
Entity ID: 208 ( context_text-Article-91197 )
Entity ID: 209 ( context_text-Person-278 )
Entity ID: 210 ( context_text-Person-281 )
Entity ID: 211 ( context_text-Person-283 )
Entity ID: 212 ( context_text-Person-287 )
Entity ID: 213 ( context_text-Person-284 )
Entity ID: 214 ( context_text-Person-282 )
Entity ID: 215 ( context_text-Person-285 )
Entity ID: 216 ( context_text-Person-280 )
Entity ID: 217 ( context_text-Person-279 )
Entity ID: 218 ( context_text-Person-286 )
Entity ID: 219 ( context_text-Article-91199 )
Entity ID: 220 ( context_text-Person-198 )
Entity ID: 221 ( context_text-Person-288 )
Entity ID: 222 ( context_text-Person-943 )
Entity ID: 223 ( context_text-Article-91254 )
Entity ID: 224 ( context_text-Person-60 )
Entity ID: 225 ( context_text-Person-972 )
Entity ID: 226 ( context_text-Person-135 )
Entity ID: 227 ( context_text-Person-314 )
Entity ID: 228 ( context_text-Person-315 )
Entity ID: 229 ( context_text-Person-313 )
Entity ID: 230 ( context_text-Article-94104 )
Entity ID: 231 ( context_text-Person-1023 )
Entity ID: 232 ( context_text-Person-1021 )
Entity ID: 233 ( context_text-Person-301 )
Entity ID: 234 ( context_text-Person-1022 )
Entity ID: 235 ( context_text-Article-94110 )
Entity ID: 236 ( context_text-Person-297 )
Entity ID: 237 ( context_text-Person-1020 )
Entity ID: 238 ( context_text-Person-1018 )
Entity ID: 239 ( context_text-Person-300 )
Entity ID: 240 ( context_text-Person-298 )
Entity ID: 241 ( context_text-Person-1017 )
Entity ID: 242 ( context_text-Person-1019 )
Entity ID: 243 ( context_text-Person-299 )
Entity ID: 244 ( context_text-Article-94125 )
Entity ID: 245 ( context_text-Person-1014 )
Entity ID: 246 ( context_text-Person-1016 )
Entity ID: 247 ( context_text-Person-277 )
Entity ID: 248 ( context_text-Person-905 )
Entity ID: 249 ( context_text-Person-1010 )
Entity ID: 250 ( context_text-Person-1011 )
Entity ID: 251 ( context_text-Person-1013 )
Entity ID: 252 ( context_text-Person-763 )
Entity ID: 253 ( context_text-Person-438 )
Entity ID: 254 ( context_text-Person-1012 )
Entity ID: 255 ( context_text-Person-1015 )
Entity ID: 256 ( context_text-Person-276 )
Entity ID: 257 ( context_text-Article-94128 )
Entity ID: 258 ( context_text-Person-293 )
Entity ID: 259 ( context_text-Person-294 )
Entity ID: 260 ( context_text-Person-296 )
Entity ID: 261 ( context_text-Article-94140 )
Entity ID: 262 ( context_text-Person-117 )
Entity ID: 263 ( context_text-Person-267 )
Entity ID: 264 ( context_text-Person-266 )
Entity ID: 265 ( context_text-Article-94301 )
Entity ID: 266 ( context_text-Person-235 )
Entity ID: 267 ( context_text-Person-241 )
Entity ID: 268 ( context_text-Person-240 )
Entity ID: 269 ( context_text-Person-238 )
Entity ID: 270 ( context_text-Article-94311 )
Entity ID: 271 ( context_text-Person-1026 )
Entity ID: 272 ( context_text-Person-1025 )
Entity ID: 273 ( context_text-Person-1028 )
Entity ID: 274 ( context_text-Person-1024 )
Entity ID: 275 ( context_text-Person-249 )
Entity ID: 276 ( context_text-Person-317 )
Entity ID: 277 ( context_text-Person-251 )
Entity ID: 278 ( context_text-Person-1027 )
Entity ID: 279 ( context_text-Person-755 )
Entity ID: 280 ( context_text-Person-253 )
Entity ID: 281 ( context_text-Person-247 )
Entity ID: 282 ( context_text-Person-256 )
Entity ID: 283 ( context_text-Person-245 )
Entity ID: 284 ( context_text-Person-254 )
Entity ID: 285 ( context_text-Person-246 )
Entity ID: 286 ( context_text-Article-94326 )
Entity ID: 287 ( context_text-Person-257 )
Entity ID: 288 ( context_text-Person-1034 )
Entity ID: 289 ( context_text-Person-1033 )
Entity ID: 290 ( context_text-Person-1037 )
Entity ID: 291 ( context_text-Person-148 )
Entity ID: 292 ( context_text-Person-1030 )
Entity ID: 293 ( context_text-Person-1031 )
Entity ID: 294 ( context_text-Person-1032 )
Entity ID: 295 ( context_text-Person-1036 )
Entity ID: 296 ( context_text-Person-1035 )
Entity ID: 297 ( context_text-Article-94405 )
Entity ID: 298 ( context_text-Person-592 )
Entity ID: 299 ( context_text-Person-1046 )
Entity ID: 300 ( context_text-Person-756 )
Entity ID: 301 ( context_text-Person-1045 )
Entity ID: 302 ( context_text-Article-94417 )
Entity ID: 303 ( context_text-Person-211 )
Entity ID: 304 ( context_text-Person-214 )
Entity ID: 305 ( context_text-Person-216 )
Entity ID: 306 ( context_text-Person-212 )
Entity ID: 307 ( context_text-Article-94430 )
Entity ID: 308 ( context_text-Person-1041 )
Entity ID: 309 ( context_text-Person-1039 )
Entity ID: 310 ( context_text-Person-1040 )
Entity ID: 311 ( context_text-Person-1042 )
Entity ID: 312 ( context_text-Article-94442 )
Entity ID: 313 ( context_text-Person-1038 )
Entity ID: 314 ( context_text-Person-203 )
Entity ID: 315 ( context_text-Person-202 )
Entity ID: 316 ( context_text-Person-201 )
Entity ID: 317 ( context_text-Person-200 )
Entity ID: 318 ( context_text-Person-199 )
Entity ID: 319 ( context_text-Person-205 )

In [40]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 171, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319)) )

TO - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 171, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319)) )

THROUGH - result count: 2743 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 171, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319)) )

FROM | TO - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 171, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 171, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319))) )

FROM | TO | THROUGH - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 171, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 171, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 WHERE U0."id" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 171, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319))) )

In [46]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "peregrine" )
test_value_list.append( "chartreuse" )
test_value_list.append( "bumblebee" )
test_value_list.append( "article" )

combined_q = ~ Q( entity_types__entity_type__slug__in = test_value_list )
entity_qs = entity_qs.filter( combined_q )

print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 272 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" WHERE NOT ("context_entity"."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee, article))))
Entity ID: 1 ( test_entity_1 )
Entity ID: 2 ( test_entity_2 )
Entity ID: 3 ( test_entity_3 )
Entity ID: 6 ( context_text-Newspaper-1 )
Entity ID: 7 ( context_text-Organization-1 )
Entity ID: 8 ( context_text-Person-161 )
Entity ID: 9 ( context_text-Person-46 )
Entity ID: 10 ( context_text-Person-872 )
Entity ID: 11 ( context_text-Person-164 )
Entity ID: 12 ( context_text-Person-165 )
Entity ID: 13 ( context_text-Person-166 )
Entity ID: 14 ( context_text-Person-186 )
Entity ID: 15 ( context_text-Person-162 )
Entity ID: 16 ( context_text-Person-163 )
Entity ID: 18 ( context_text-Person-30 )
Entity ID: 19 ( context_text-Person-750 )
Entity ID: 20 ( context_text-Person-175 )
Entity ID: 21 ( context_text-Person-174 )
Entity ID: 22 ( context_text-Person-176 )
Entity ID: 23 ( context_text-Person-173 )
Entity ID: 24 ( context_text-Person-172 )
Entity ID: 25 ( context_text-Person-32 )
Entity ID: 27 ( context_text-Person-877 )
Entity ID: 28 ( context_text-Person-182 )
Entity ID: 29 ( context_text-Person-878 )
Entity ID: 30 ( context_text-Person-180 )
Entity ID: 31 ( context_text-Person-876 )
Entity ID: 32 ( context_text-Person-181 )
Entity ID: 33 ( context_text-Person-875 )
Entity ID: 35 ( context_text-Person-178 )
Entity ID: 36 ( context_text-Person-179 )
Entity ID: 38 ( context_text-Person-913 )
Entity ID: 39 ( context_text-Person-914 )
Entity ID: 40 ( context_text-Person-234 )
Entity ID: 41 ( context_text-Person-909 )
Entity ID: 42 ( context_text-Person-213 )
Entity ID: 43 ( context_text-Person-910 )
Entity ID: 44 ( context_text-Person-132 )
Entity ID: 45 ( context_text-Person-912 )
Entity ID: 46 ( context_text-Person-215 )
Entity ID: 47 ( context_text-Person-911 )
Entity ID: 49 ( context_text-Person-73 )
Entity ID: 50 ( context_text-Person-901 )
Entity ID: 51 ( context_text-Person-237 )
Entity ID: 52 ( context_text-Person-236 )
Entity ID: 53 ( context_text-Person-239 )
Entity ID: 55 ( context_text-Person-23 )
Entity ID: 56 ( context_text-Person-935 )
Entity ID: 57 ( context_text-Person-933 )
Entity ID: 58 ( context_text-Person-269 )
Entity ID: 59 ( context_text-Person-268 )
Entity ID: 60 ( context_text-Person-934 )
Entity ID: 61 ( context_text-Person-1049 )
Entity ID: 171 ( context_text-Person-48 )
Entity ID: 63 ( context_text-Person-36 )
Entity ID: 64 ( context_text-Person-272 )
Entity ID: 65 ( context_text-Person-275 )
Entity ID: 66 ( context_text-Person-274 )
Entity ID: 67 ( context_text-Person-273 )
Entity ID: 69 ( context_text-Person-69 )
Entity ID: 70 ( context_text-Person-289 )
Entity ID: 71 ( context_text-Person-290 )
Entity ID: 72 ( context_text-Person-102 )
Entity ID: 74 ( context_text-Person-217 )
Entity ID: 75 ( context_text-Person-753 )
Entity ID: 76 ( context_text-Person-292 )
Entity ID: 77 ( context_text-Person-295 )
Entity ID: 78 ( context_text-Person-752 )
Entity ID: 80 ( context_text-Person-591 )
Entity ID: 81 ( context_text-Person-312 )
Entity ID: 82 ( context_text-Person-311 )
Entity ID: 83 ( context_text-Person-310 )
Entity ID: 85 ( context_text-Person-84 )
Entity ID: 86 ( context_text-Person-1002 )
Entity ID: 87 ( context_text-Person-308 )
Entity ID: 88 ( context_text-Person-1003 )
Entity ID: 89 ( context_text-Person-307 )
Entity ID: 90 ( context_text-Person-1001 )
Entity ID: 92 ( context_text-Person-302 )
Entity ID: 93 ( context_text-Person-306 )
Entity ID: 94 ( context_text-Person-1009 )
Entity ID: 95 ( context_text-Person-304 )
Entity ID: 96 ( context_text-Person-303 )
Entity ID: 97 ( context_text-Person-305 )
Entity ID: 99 ( context_text-Person-66 )
Entity ID: 100 ( context_text-Person-754 )
Entity ID: 101 ( context_text-Person-263 )
Entity ID: 102 ( context_text-Person-261 )
Entity ID: 103 ( context_text-Person-259 )
Entity ID: 105 ( context_text-Person-223 )
Entity ID: 106 ( context_text-Person-225 )
Entity ID: 107 ( context_text-Person-227 )
Entity ID: 108 ( context_text-Person-229 )
Entity ID: 109 ( context_text-Person-228 )
Entity ID: 110 ( context_text-Person-226 )
Entity ID: 111 ( context_text-Person-1029 )
Entity ID: 112 ( context_text-Person-224 )
Entity ID: 113 ( context_text-Person-230 )
Entity ID: 115 ( context_text-Person-222 )
Entity ID: 117 ( context_text-Person-232 )
Entity ID: 118 ( context_text-Person-231 )
Entity ID: 119 ( context_text-Person-233 )
Entity ID: 121 ( context_text-Person-221 )
Entity ID: 122 ( context_text-Person-220 )
Entity ID: 123 ( context_text-Person-219 )
Entity ID: 124 ( context_text-Person-218 )
Entity ID: 126 ( context_text-Person-1043 )
Entity ID: 127 ( context_text-Person-980 )
Entity ID: 128 ( context_text-Person-1044 )
Entity ID: 129 ( context_text-Person-188 )
Entity ID: 130 ( context_text-Person-187 )
Entity ID: 131 ( context_text-Person-189 )
Entity ID: 133 ( context_text-Newspaper-2 )
Entity ID: 134 ( context_text-Organization-4 )
Entity ID: 135 ( context_text-Person-155 )
Entity ID: 136 ( context_text-Person-159 )
Entity ID: 137 ( context_text-Person-160 )
Entity ID: 138 ( context_text-Person-156 )
Entity ID: 139 ( context_text-Person-158 )
Entity ID: 140 ( context_text-Person-157 )
Entity ID: 142 ( context_text-Person-167 )
Entity ID: 143 ( context_text-Person-169 )
Entity ID: 144 ( context_text-Person-168 )
Entity ID: 145 ( context_text-Person-889 )
Entity ID: 147 ( context_text-Person-56 )
Entity ID: 148 ( context_text-Person-171 )
Entity ID: 149 ( context_text-Person-874 )
Entity ID: 150 ( context_text-Person-873 )
Entity ID: 151 ( context_text-Person-170 )
Entity ID: 153 ( context_text-Person-143 )
Entity ID: 154 ( context_text-Person-194 )
Entity ID: 155 ( context_text-Person-197 )
Entity ID: 156 ( context_text-Person-854 )
Entity ID: 157 ( context_text-Person-196 )
Entity ID: 158 ( context_text-Person-193 )
Entity ID: 159 ( context_text-Person-195 )
Entity ID: 161 ( context_text-Person-183 )
Entity ID: 162 ( context_text-Person-184 )
Entity ID: 163 ( context_text-Person-185 )
Entity ID: 165 ( context_text-Person-190 )
Entity ID: 166 ( context_text-Person-906 )
Entity ID: 167 ( context_text-Person-751 )
Entity ID: 168 ( context_text-Person-191 )
Entity ID: 169 ( context_text-Person-907 )
Entity ID: 172 ( context_text-Person-204 )
Entity ID: 173 ( context_text-Person-206 )
Entity ID: 174 ( context_text-Person-207 )
Entity ID: 175 ( context_text-Person-210 )
Entity ID: 176 ( context_text-Person-209 )
Entity ID: 178 ( context_text-Person-262 )
Entity ID: 179 ( context_text-Person-925 )
Entity ID: 180 ( context_text-Person-264 )
Entity ID: 181 ( context_text-Person-265 )
Entity ID: 182 ( context_text-Person-924 )
Entity ID: 183 ( context_text-Person-922 )
Entity ID: 184 ( context_text-Person-921 )
Entity ID: 185 ( context_text-Person-923 )
Entity ID: 188 ( context_text-Person-260 )
Entity ID: 189 ( context_text-Person-919 )
Entity ID: 190 ( context_text-Person-920 )
Entity ID: 192 ( context_text-Person-90 )
Entity ID: 193 ( context_text-Person-938 )
Entity ID: 194 ( context_text-Person-244 )
Entity ID: 195 ( context_text-Person-242 )
Entity ID: 196 ( context_text-Person-940 )
Entity ID: 197 ( context_text-Person-942 )
Entity ID: 198 ( context_text-Person-939 )
Entity ID: 199 ( context_text-Person-941 )
Entity ID: 200 ( context_text-Person-316 )
Entity ID: 201 ( context_text-Person-243 )
Entity ID: 203 ( context_text-Person-255 )
Entity ID: 204 ( context_text-Person-248 )
Entity ID: 205 ( context_text-Person-926 )
Entity ID: 206 ( context_text-Person-250 )
Entity ID: 207 ( context_text-Person-927 )
Entity ID: 209 ( context_text-Person-278 )
Entity ID: 210 ( context_text-Person-281 )
Entity ID: 211 ( context_text-Person-283 )
Entity ID: 212 ( context_text-Person-287 )
Entity ID: 213 ( context_text-Person-284 )
Entity ID: 214 ( context_text-Person-282 )
Entity ID: 215 ( context_text-Person-285 )
Entity ID: 216 ( context_text-Person-280 )
Entity ID: 217 ( context_text-Person-279 )
Entity ID: 218 ( context_text-Person-286 )
Entity ID: 220 ( context_text-Person-198 )
Entity ID: 221 ( context_text-Person-288 )
Entity ID: 222 ( context_text-Person-943 )
Entity ID: 224 ( context_text-Person-60 )
Entity ID: 225 ( context_text-Person-972 )
Entity ID: 226 ( context_text-Person-135 )
Entity ID: 227 ( context_text-Person-314 )
Entity ID: 228 ( context_text-Person-315 )
Entity ID: 229 ( context_text-Person-313 )
Entity ID: 231 ( context_text-Person-1023 )
Entity ID: 232 ( context_text-Person-1021 )
Entity ID: 233 ( context_text-Person-301 )
Entity ID: 234 ( context_text-Person-1022 )
Entity ID: 236 ( context_text-Person-297 )
Entity ID: 237 ( context_text-Person-1020 )
Entity ID: 238 ( context_text-Person-1018 )
Entity ID: 239 ( context_text-Person-300 )
Entity ID: 240 ( context_text-Person-298 )
Entity ID: 241 ( context_text-Person-1017 )
Entity ID: 242 ( context_text-Person-1019 )
Entity ID: 243 ( context_text-Person-299 )
Entity ID: 245 ( context_text-Person-1014 )
Entity ID: 246 ( context_text-Person-1016 )
Entity ID: 247 ( context_text-Person-277 )
Entity ID: 248 ( context_text-Person-905 )
Entity ID: 249 ( context_text-Person-1010 )
Entity ID: 250 ( context_text-Person-1011 )
Entity ID: 251 ( context_text-Person-1013 )
Entity ID: 252 ( context_text-Person-763 )
Entity ID: 253 ( context_text-Person-438 )
Entity ID: 254 ( context_text-Person-1012 )
Entity ID: 255 ( context_text-Person-1015 )
Entity ID: 256 ( context_text-Person-276 )
Entity ID: 258 ( context_text-Person-293 )
Entity ID: 259 ( context_text-Person-294 )
Entity ID: 260 ( context_text-Person-296 )
Entity ID: 262 ( context_text-Person-117 )
Entity ID: 263 ( context_text-Person-267 )
Entity ID: 264 ( context_text-Person-266 )
Entity ID: 266 ( context_text-Person-235 )
Entity ID: 267 ( context_text-Person-241 )
Entity ID: 268 ( context_text-Person-240 )
Entity ID: 269 ( context_text-Person-238 )
Entity ID: 271 ( context_text-Person-1026 )
Entity ID: 272 ( context_text-Person-1025 )
Entity ID: 273 ( context_text-Person-1028 )
Entity ID: 274 ( context_text-Person-1024 )
Entity ID: 275 ( context_text-Person-249 )
Entity ID: 276 ( context_text-Person-317 )
Entity ID: 277 ( context_text-Person-251 )
Entity ID: 278 ( context_text-Person-1027 )
Entity ID: 279 ( context_text-Person-755 )
Entity ID: 280 ( context_text-Person-253 )
Entity ID: 281 ( context_text-Person-247 )
Entity ID: 282 ( context_text-Person-256 )
Entity ID: 283 ( context_text-Person-245 )
Entity ID: 284 ( context_text-Person-254 )
Entity ID: 285 ( context_text-Person-246 )
Entity ID: 287 ( context_text-Person-257 )
Entity ID: 288 ( context_text-Person-1034 )
Entity ID: 289 ( context_text-Person-1033 )
Entity ID: 290 ( context_text-Person-1037 )
Entity ID: 291 ( context_text-Person-148 )
Entity ID: 292 ( context_text-Person-1030 )
Entity ID: 293 ( context_text-Person-1031 )
Entity ID: 294 ( context_text-Person-1032 )
Entity ID: 295 ( context_text-Person-1036 )
Entity ID: 296 ( context_text-Person-1035 )
Entity ID: 298 ( context_text-Person-592 )
Entity ID: 299 ( context_text-Person-1046 )
Entity ID: 300 ( context_text-Person-756 )
Entity ID: 301 ( context_text-Person-1045 )
Entity ID: 303 ( context_text-Person-211 )
Entity ID: 304 ( context_text-Person-214 )
Entity ID: 305 ( context_text-Person-216 )
Entity ID: 306 ( context_text-Person-212 )
Entity ID: 308 ( context_text-Person-1041 )
Entity ID: 309 ( context_text-Person-1039 )
Entity ID: 310 ( context_text-Person-1040 )
Entity ID: 311 ( context_text-Person-1042 )
Entity ID: 313 ( context_text-Person-1038 )
Entity ID: 314 ( context_text-Person-203 )
Entity ID: 315 ( context_text-Person-202 )
Entity ID: 316 ( context_text-Person-201 )
Entity ID: 317 ( context_text-Person-200 )
Entity ID: 318 ( context_text-Person-199 )
Entity ID: 319 ( context_text-Person-205 )

In [47]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 2789 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT V0."id" FROM "context_entity" V0 WHERE NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee, article)))) )

TO - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT V0."id" FROM "context_entity" V0 WHERE NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee, article)))) )

THROUGH - result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT V0."id" FROM "context_entity" V0 WHERE NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee, article)))) )

FROM | TO - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT V0."id" FROM "context_entity" V0 WHERE NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee, article)))) OR "context_entity_relation"."relation_to_id" IN (SELECT V0."id" FROM "context_entity" V0 WHERE NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee, article))))) )

FROM | TO | THROUGH - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT V0."id" FROM "context_entity" V0 WHERE NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee, article)))) OR "context_entity_relation"."relation_to_id" IN (SELECT V0."id" FROM "context_entity" V0 WHERE NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee, article)))) OR "context_entity_relation"."relation_through_id" IN (SELECT V0."id" FROM "context_entity" V0 WHERE NOT (V0."id" IN (SELECT U1."entity_id" FROM "context_entity_types" U1 INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (peregrine, chartreuse, bumblebee, article))))) )

filter on relation traits


In [48]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )

name_q = Q( entity_relation_trait__name = "pub_date" )
value_q = Q( entity_relation_trait__value = "2009-12-07" )
combined_q = name_q & value_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 167 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" = 2009-12-07) )

test IN list of pub_date values


In [77]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )

test_value_list = []
test_value_list.append( "20202020" )
test_value_list.append( "20202021" )
test_value_list.append( "20202022" )

name_q = Q( entity_relation_trait__name = "pub_date" )
value_q = Q( entity_relation_trait__value__in = test_value_list )
combined_q = name_q & value_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" IN (20202020, 20202021, 20202022)) )

In [79]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )

test_value_list = []
test_value_list.append( "2009-12-07" )
test_value_list.append( "2010-02-08" )
test_value_list.append( "2010-02-13" )

name_q = Q( entity_relation_trait__name = "pub_date" )
value_q = Q( entity_relation_trait__value__in = test_value_list )
combined_q = name_q & value_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 793 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" IN (2009-12-07, 2010-02-08, 2010-02-13)) )

test NOT IN list of pub_date values


In [78]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )

test_value_list = []
test_value_list.append( "20202020" )
test_value_list.append( "20202021" )
test_value_list.append( "20202022" )

name_q = Q( entity_relation_trait__name = "pub_date" )
value_q = ~ Q( entity_relation_trait__value__in = test_value_list )
combined_q = name_q & value_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 3211 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND NOT ("context_entity_relation"."id" IN (SELECT U1."entity_relation_id" FROM "context_entity_relation_trait" U1 WHERE (U1."value" IN (20202020, 20202021, 20202022) AND U1."id" = ("context_entity_relation_trait"."id"))))) )

In [67]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )

test_value_list = []
test_value_list.append( "2009-12-07" )
test_value_list.append( "2010-02-08" )
test_value_list.append( "2010-02-13" )

name_q = Q( entity_relation_trait__name = "pub_date" )
value_q = ~ Q( entity_relation_trait__value__in = test_value_list )
combined_q = name_q & value_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 2418 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND NOT ("context_entity_relation"."id" IN (SELECT U1."entity_relation_id" FROM "context_entity_relation_trait" U1 WHERE (U1."value" IN (2009-12-07, 2010-02-08, 2010-02-13) AND U1."id" = ("context_entity_relation_trait"."id"))))) )

test IN RANGE for pub_date values


In [80]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )

name_q = Q( entity_relation_trait__name = "pub_date" )
left_range_q = Q( entity_relation_trait__value__gte = "2019-12-01" )
right_range_q = Q( entity_relation_trait__value__lte = "2019-12-31" )
combined_q = name_q & left_range_q & right_range_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2019-12-01 AND "context_entity_relation_trait"."value" <= 2019-12-31) )

In [72]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )

name_q = Q( entity_relation_trait__name = "pub_date" )
left_range_q = Q( entity_relation_trait__value__gte = "2010-02-08" )
right_range_q = Q( entity_relation_trait__value__lte = "2010-02-13" )
combined_q = name_q & left_range_q & right_range_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 1443 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2010-02-08 AND "context_entity_relation_trait"."value" <= 2010-02-13) )

filter on relation type slug


In [49]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )

name_q = Q( relation_type__slug = "quoted" )
combined_q = name_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 155 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") WHERE "context_entity_relation_type"."slug" = quoted )

test IN list of relation types


In [68]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )
test_value_list = []
test_value_list.append( "quoted" )
test_value_list.append( "mentioned" )
test_value_list.append( "shared_byline" )

name_q = Q( relation_type__slug__in = test_value_list )
combined_q = name_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 437 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") WHERE "context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) )

In [69]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )
test_value_list = []
test_value_list.append( "quoted" )
test_value_list.append( "mentioned" )
test_value_list.append( "shared_byline" )

name_q = ~ Q( relation_type__slug__in = test_value_list )
combined_q = name_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 2778 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" LEFT OUTER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") WHERE NOT ("context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) AND "context_entity_relation_type"."slug" IS NOT NULL) )

test FROM-TO-THROUGH function


In [90]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "person" )
test_value_list.append( "article" )

combined_q = Q( entity_types__entity_type__slug__in = test_value_list )
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 315 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_types" ON ("context_entity"."id" = "context_entity_types"."entity_id") INNER JOIN "context_entity_type" ON ("context_entity_types"."entity_type_id" = "context_entity_type"."id") WHERE "context_entity_type"."slug" IN (person, article))
Entity ID: 1 ( test_entity_1 )
Entity ID: 2 ( test_entity_2 )
Entity ID: 3 ( test_entity_3 )
Entity ID: 4 ( test_entity_4 )
Entity ID: 5 ( context_text-Article-21409 )
Entity ID: 8 ( context_text-Person-161 )
Entity ID: 9 ( context_text-Person-46 )
Entity ID: 10 ( context_text-Person-872 )
Entity ID: 11 ( context_text-Person-164 )
Entity ID: 12 ( context_text-Person-165 )
Entity ID: 13 ( context_text-Person-166 )
Entity ID: 14 ( context_text-Person-186 )
Entity ID: 15 ( context_text-Person-162 )
Entity ID: 16 ( context_text-Person-163 )
Entity ID: 17 ( context_text-Article-21483 )
Entity ID: 18 ( context_text-Person-30 )
Entity ID: 19 ( context_text-Person-750 )
Entity ID: 20 ( context_text-Person-175 )
Entity ID: 21 ( context_text-Person-174 )
Entity ID: 22 ( context_text-Person-176 )
Entity ID: 23 ( context_text-Person-173 )
Entity ID: 24 ( context_text-Person-172 )
Entity ID: 25 ( context_text-Person-32 )
Entity ID: 26 ( context_text-Article-21509 )
Entity ID: 27 ( context_text-Person-877 )
Entity ID: 28 ( context_text-Person-182 )
Entity ID: 29 ( context_text-Person-878 )
Entity ID: 30 ( context_text-Person-180 )
Entity ID: 31 ( context_text-Person-876 )
Entity ID: 32 ( context_text-Person-181 )
Entity ID: 33 ( context_text-Person-875 )
Entity ID: 34 ( context_text-Article-21512 )
Entity ID: 35 ( context_text-Person-178 )
Entity ID: 36 ( context_text-Person-179 )
Entity ID: 37 ( context_text-Article-21627 )
Entity ID: 38 ( context_text-Person-913 )
Entity ID: 39 ( context_text-Person-914 )
Entity ID: 40 ( context_text-Person-234 )
Entity ID: 41 ( context_text-Person-909 )
Entity ID: 42 ( context_text-Person-213 )
Entity ID: 43 ( context_text-Person-910 )
Entity ID: 44 ( context_text-Person-132 )
Entity ID: 45 ( context_text-Person-912 )
Entity ID: 46 ( context_text-Person-215 )
Entity ID: 47 ( context_text-Person-911 )
Entity ID: 48 ( context_text-Article-21661 )
Entity ID: 49 ( context_text-Person-73 )
Entity ID: 50 ( context_text-Person-901 )
Entity ID: 51 ( context_text-Person-237 )
Entity ID: 52 ( context_text-Person-236 )
Entity ID: 53 ( context_text-Person-239 )
Entity ID: 54 ( context_text-Article-21719 )
Entity ID: 55 ( context_text-Person-23 )
Entity ID: 56 ( context_text-Person-935 )
Entity ID: 57 ( context_text-Person-933 )
Entity ID: 58 ( context_text-Person-269 )
Entity ID: 59 ( context_text-Person-268 )
Entity ID: 60 ( context_text-Person-934 )
Entity ID: 61 ( context_text-Person-1049 )
Entity ID: 62 ( context_text-Article-21738 )
Entity ID: 63 ( context_text-Person-36 )
Entity ID: 64 ( context_text-Person-272 )
Entity ID: 65 ( context_text-Person-275 )
Entity ID: 66 ( context_text-Person-274 )
Entity ID: 67 ( context_text-Person-273 )
Entity ID: 68 ( context_text-Article-21790 )
Entity ID: 69 ( context_text-Person-69 )
Entity ID: 70 ( context_text-Person-289 )
Entity ID: 71 ( context_text-Person-290 )
Entity ID: 72 ( context_text-Person-102 )
Entity ID: 73 ( context_text-Article-21827 )
Entity ID: 74 ( context_text-Person-217 )
Entity ID: 75 ( context_text-Person-753 )
Entity ID: 76 ( context_text-Person-292 )
Entity ID: 77 ( context_text-Person-295 )
Entity ID: 78 ( context_text-Person-752 )
Entity ID: 79 ( context_text-Article-21890 )
Entity ID: 80 ( context_text-Person-591 )
Entity ID: 81 ( context_text-Person-312 )
Entity ID: 82 ( context_text-Person-311 )
Entity ID: 83 ( context_text-Person-310 )
Entity ID: 84 ( context_text-Article-21925 )
Entity ID: 85 ( context_text-Person-84 )
Entity ID: 86 ( context_text-Person-1002 )
Entity ID: 87 ( context_text-Person-308 )
Entity ID: 88 ( context_text-Person-1003 )
Entity ID: 89 ( context_text-Person-307 )
Entity ID: 90 ( context_text-Person-1001 )
Entity ID: 91 ( context_text-Article-28274 )
Entity ID: 92 ( context_text-Person-302 )
Entity ID: 93 ( context_text-Person-306 )
Entity ID: 94 ( context_text-Person-1009 )
Entity ID: 95 ( context_text-Person-304 )
Entity ID: 96 ( context_text-Person-303 )
Entity ID: 97 ( context_text-Person-305 )
Entity ID: 98 ( context_text-Article-28499 )
Entity ID: 99 ( context_text-Person-66 )
Entity ID: 100 ( context_text-Person-754 )
Entity ID: 101 ( context_text-Person-263 )
Entity ID: 102 ( context_text-Person-261 )
Entity ID: 103 ( context_text-Person-259 )
Entity ID: 104 ( context_text-Article-28598 )
Entity ID: 105 ( context_text-Person-223 )
Entity ID: 106 ( context_text-Person-225 )
Entity ID: 107 ( context_text-Person-227 )
Entity ID: 108 ( context_text-Person-229 )
Entity ID: 109 ( context_text-Person-228 )
Entity ID: 110 ( context_text-Person-226 )
Entity ID: 111 ( context_text-Person-1029 )
Entity ID: 112 ( context_text-Person-224 )
Entity ID: 113 ( context_text-Person-230 )
Entity ID: 114 ( context_text-Article-28610 )
Entity ID: 115 ( context_text-Person-222 )
Entity ID: 116 ( context_text-Article-28649 )
Entity ID: 117 ( context_text-Person-232 )
Entity ID: 118 ( context_text-Person-231 )
Entity ID: 119 ( context_text-Person-233 )
Entity ID: 120 ( context_text-Article-28741 )
Entity ID: 121 ( context_text-Person-221 )
Entity ID: 122 ( context_text-Person-220 )
Entity ID: 123 ( context_text-Person-219 )
Entity ID: 124 ( context_text-Person-218 )
Entity ID: 125 ( context_text-Article-28846 )
Entity ID: 126 ( context_text-Person-1043 )
Entity ID: 127 ( context_text-Person-980 )
Entity ID: 128 ( context_text-Person-1044 )
Entity ID: 129 ( context_text-Person-188 )
Entity ID: 130 ( context_text-Person-187 )
Entity ID: 131 ( context_text-Person-189 )
Entity ID: 132 ( context_text-Article-90948 )
Entity ID: 135 ( context_text-Person-155 )
Entity ID: 136 ( context_text-Person-159 )
Entity ID: 137 ( context_text-Person-160 )
Entity ID: 138 ( context_text-Person-156 )
Entity ID: 139 ( context_text-Person-158 )
Entity ID: 140 ( context_text-Person-157 )
Entity ID: 141 ( context_text-Article-90983 )
Entity ID: 142 ( context_text-Person-167 )
Entity ID: 143 ( context_text-Person-169 )
Entity ID: 144 ( context_text-Person-168 )
Entity ID: 145 ( context_text-Person-889 )
Entity ID: 146 ( context_text-Article-91000 )
Entity ID: 147 ( context_text-Person-56 )
Entity ID: 148 ( context_text-Person-171 )
Entity ID: 149 ( context_text-Person-874 )
Entity ID: 150 ( context_text-Person-873 )
Entity ID: 151 ( context_text-Person-170 )
Entity ID: 152 ( context_text-Article-91036 )
Entity ID: 153 ( context_text-Person-143 )
Entity ID: 154 ( context_text-Person-194 )
Entity ID: 155 ( context_text-Person-197 )
Entity ID: 156 ( context_text-Person-854 )
Entity ID: 157 ( context_text-Person-196 )
Entity ID: 158 ( context_text-Person-193 )
Entity ID: 159 ( context_text-Person-195 )
Entity ID: 160 ( context_text-Article-91038 )
Entity ID: 161 ( context_text-Person-183 )
Entity ID: 162 ( context_text-Person-184 )
Entity ID: 163 ( context_text-Person-185 )
Entity ID: 164 ( context_text-Article-91068 )
Entity ID: 165 ( context_text-Person-190 )
Entity ID: 166 ( context_text-Person-906 )
Entity ID: 167 ( context_text-Person-751 )
Entity ID: 168 ( context_text-Person-191 )
Entity ID: 169 ( context_text-Person-907 )
Entity ID: 170 ( context_text-Article-91071 )
Entity ID: 171 ( context_text-Person-48 )
Entity ID: 172 ( context_text-Person-204 )
Entity ID: 173 ( context_text-Person-206 )
Entity ID: 174 ( context_text-Person-207 )
Entity ID: 175 ( context_text-Person-210 )
Entity ID: 176 ( context_text-Person-209 )
Entity ID: 177 ( context_text-Article-91112 )
Entity ID: 178 ( context_text-Person-262 )
Entity ID: 179 ( context_text-Person-925 )
Entity ID: 180 ( context_text-Person-264 )
Entity ID: 181 ( context_text-Person-265 )
Entity ID: 182 ( context_text-Person-924 )
Entity ID: 183 ( context_text-Person-922 )
Entity ID: 184 ( context_text-Person-921 )
Entity ID: 185 ( context_text-Person-923 )
Entity ID: 186 ( context_text-Article-91114 )
Entity ID: 187 ( context_text-Article-91132 )
Entity ID: 188 ( context_text-Person-260 )
Entity ID: 189 ( context_text-Person-919 )
Entity ID: 190 ( context_text-Person-920 )
Entity ID: 191 ( context_text-Article-91133 )
Entity ID: 192 ( context_text-Person-90 )
Entity ID: 193 ( context_text-Person-938 )
Entity ID: 194 ( context_text-Person-244 )
Entity ID: 195 ( context_text-Person-242 )
Entity ID: 196 ( context_text-Person-940 )
Entity ID: 197 ( context_text-Person-942 )
Entity ID: 198 ( context_text-Person-939 )
Entity ID: 199 ( context_text-Person-941 )
Entity ID: 200 ( context_text-Person-316 )
Entity ID: 201 ( context_text-Person-243 )
Entity ID: 202 ( context_text-Article-91157 )
Entity ID: 203 ( context_text-Person-255 )
Entity ID: 204 ( context_text-Person-248 )
Entity ID: 205 ( context_text-Person-926 )
Entity ID: 206 ( context_text-Person-250 )
Entity ID: 207 ( context_text-Person-927 )
Entity ID: 208 ( context_text-Article-91197 )
Entity ID: 209 ( context_text-Person-278 )
Entity ID: 210 ( context_text-Person-281 )
Entity ID: 211 ( context_text-Person-283 )
Entity ID: 212 ( context_text-Person-287 )
Entity ID: 213 ( context_text-Person-284 )
Entity ID: 214 ( context_text-Person-282 )
Entity ID: 215 ( context_text-Person-285 )
Entity ID: 216 ( context_text-Person-280 )
Entity ID: 217 ( context_text-Person-279 )
Entity ID: 218 ( context_text-Person-286 )
Entity ID: 219 ( context_text-Article-91199 )
Entity ID: 220 ( context_text-Person-198 )
Entity ID: 221 ( context_text-Person-288 )
Entity ID: 222 ( context_text-Person-943 )
Entity ID: 223 ( context_text-Article-91254 )
Entity ID: 224 ( context_text-Person-60 )
Entity ID: 225 ( context_text-Person-972 )
Entity ID: 226 ( context_text-Person-135 )
Entity ID: 227 ( context_text-Person-314 )
Entity ID: 228 ( context_text-Person-315 )
Entity ID: 229 ( context_text-Person-313 )
Entity ID: 230 ( context_text-Article-94104 )
Entity ID: 231 ( context_text-Person-1023 )
Entity ID: 232 ( context_text-Person-1021 )
Entity ID: 233 ( context_text-Person-301 )
Entity ID: 234 ( context_text-Person-1022 )
Entity ID: 235 ( context_text-Article-94110 )
Entity ID: 236 ( context_text-Person-297 )
Entity ID: 237 ( context_text-Person-1020 )
Entity ID: 238 ( context_text-Person-1018 )
Entity ID: 239 ( context_text-Person-300 )
Entity ID: 240 ( context_text-Person-298 )
Entity ID: 241 ( context_text-Person-1017 )
Entity ID: 242 ( context_text-Person-1019 )
Entity ID: 243 ( context_text-Person-299 )
Entity ID: 244 ( context_text-Article-94125 )
Entity ID: 245 ( context_text-Person-1014 )
Entity ID: 246 ( context_text-Person-1016 )
Entity ID: 247 ( context_text-Person-277 )
Entity ID: 248 ( context_text-Person-905 )
Entity ID: 249 ( context_text-Person-1010 )
Entity ID: 250 ( context_text-Person-1011 )
Entity ID: 251 ( context_text-Person-1013 )
Entity ID: 252 ( context_text-Person-763 )
Entity ID: 253 ( context_text-Person-438 )
Entity ID: 254 ( context_text-Person-1012 )
Entity ID: 255 ( context_text-Person-1015 )
Entity ID: 256 ( context_text-Person-276 )
Entity ID: 257 ( context_text-Article-94128 )
Entity ID: 258 ( context_text-Person-293 )
Entity ID: 259 ( context_text-Person-294 )
Entity ID: 260 ( context_text-Person-296 )
Entity ID: 261 ( context_text-Article-94140 )
Entity ID: 262 ( context_text-Person-117 )
Entity ID: 263 ( context_text-Person-267 )
Entity ID: 264 ( context_text-Person-266 )
Entity ID: 265 ( context_text-Article-94301 )
Entity ID: 266 ( context_text-Person-235 )
Entity ID: 267 ( context_text-Person-241 )
Entity ID: 268 ( context_text-Person-240 )
Entity ID: 269 ( context_text-Person-238 )
Entity ID: 270 ( context_text-Article-94311 )
Entity ID: 271 ( context_text-Person-1026 )
Entity ID: 272 ( context_text-Person-1025 )
Entity ID: 273 ( context_text-Person-1028 )
Entity ID: 274 ( context_text-Person-1024 )
Entity ID: 275 ( context_text-Person-249 )
Entity ID: 276 ( context_text-Person-317 )
Entity ID: 277 ( context_text-Person-251 )
Entity ID: 278 ( context_text-Person-1027 )
Entity ID: 279 ( context_text-Person-755 )
Entity ID: 280 ( context_text-Person-253 )
Entity ID: 281 ( context_text-Person-247 )
Entity ID: 282 ( context_text-Person-256 )
Entity ID: 283 ( context_text-Person-245 )
Entity ID: 284 ( context_text-Person-254 )
Entity ID: 285 ( context_text-Person-246 )
Entity ID: 286 ( context_text-Article-94326 )
Entity ID: 287 ( context_text-Person-257 )
Entity ID: 288 ( context_text-Person-1034 )
Entity ID: 289 ( context_text-Person-1033 )
Entity ID: 290 ( context_text-Person-1037 )
Entity ID: 291 ( context_text-Person-148 )
Entity ID: 292 ( context_text-Person-1030 )
Entity ID: 293 ( context_text-Person-1031 )
Entity ID: 294 ( context_text-Person-1032 )
Entity ID: 295 ( context_text-Person-1036 )
Entity ID: 296 ( context_text-Person-1035 )
Entity ID: 297 ( context_text-Article-94405 )
Entity ID: 298 ( context_text-Person-592 )
Entity ID: 299 ( context_text-Person-1046 )
Entity ID: 300 ( context_text-Person-756 )
Entity ID: 301 ( context_text-Person-1045 )
Entity ID: 302 ( context_text-Article-94417 )
Entity ID: 303 ( context_text-Person-211 )
Entity ID: 304 ( context_text-Person-214 )
Entity ID: 305 ( context_text-Person-216 )
Entity ID: 306 ( context_text-Person-212 )
Entity ID: 307 ( context_text-Article-94430 )
Entity ID: 308 ( context_text-Person-1041 )
Entity ID: 309 ( context_text-Person-1039 )
Entity ID: 310 ( context_text-Person-1040 )
Entity ID: 311 ( context_text-Person-1042 )
Entity ID: 312 ( context_text-Article-94442 )
Entity ID: 313 ( context_text-Person-1038 )
Entity ID: 314 ( context_text-Person-203 )
Entity ID: 315 ( context_text-Person-202 )
Entity ID: 316 ( context_text-Person-201 )
Entity ID: 317 ( context_text-Person-200 )
Entity ID: 318 ( context_text-Person-199 )
Entity ID: 319 ( context_text-Person-205 )

In [91]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | THROUGH
combined_q = from_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO | THROUGH
combined_q = to_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nTO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = from_q | to_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 2746 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) )

TO - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) )

THROUGH - result count: 2743 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) )

FROM | TO - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article))) )

FROM | THROUGH - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article))) )

TO | THROUGH - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article))) )

FROM | TO | THROUGH - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (person, article))) )

In [92]:
# try to just find the entity with the desired identifiers.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

test_value_list = []
test_value_list.append( "newspaper" )
test_value_list.append( "article" )

combined_q = Q( entity_types__entity_type__slug__in = test_value_list )
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# loop to retrieve IDs.
entity_id_list = []
for entity in entity_qs:
    
    # get, print, and store ID.
    entity_id = entity.id
    print( "Entity ID: {} ( {} )".format( entity_id, entity.name ) )
    entity_id_list.append( entity_id )
    
#-- END loop over matching entities. --#


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 49 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_types" ON ("context_entity"."id" = "context_entity_types"."entity_id") INNER JOIN "context_entity_type" ON ("context_entity_types"."entity_type_id" = "context_entity_type"."id") WHERE "context_entity_type"."slug" IN (newspaper, article))
Entity ID: 4 ( test_entity_4 )
Entity ID: 5 ( context_text-Article-21409 )
Entity ID: 6 ( context_text-Newspaper-1 )
Entity ID: 17 ( context_text-Article-21483 )
Entity ID: 26 ( context_text-Article-21509 )
Entity ID: 34 ( context_text-Article-21512 )
Entity ID: 37 ( context_text-Article-21627 )
Entity ID: 48 ( context_text-Article-21661 )
Entity ID: 54 ( context_text-Article-21719 )
Entity ID: 62 ( context_text-Article-21738 )
Entity ID: 68 ( context_text-Article-21790 )
Entity ID: 73 ( context_text-Article-21827 )
Entity ID: 79 ( context_text-Article-21890 )
Entity ID: 84 ( context_text-Article-21925 )
Entity ID: 91 ( context_text-Article-28274 )
Entity ID: 98 ( context_text-Article-28499 )
Entity ID: 104 ( context_text-Article-28598 )
Entity ID: 114 ( context_text-Article-28610 )
Entity ID: 116 ( context_text-Article-28649 )
Entity ID: 120 ( context_text-Article-28741 )
Entity ID: 125 ( context_text-Article-28846 )
Entity ID: 132 ( context_text-Article-90948 )
Entity ID: 133 ( context_text-Newspaper-2 )
Entity ID: 141 ( context_text-Article-90983 )
Entity ID: 146 ( context_text-Article-91000 )
Entity ID: 152 ( context_text-Article-91036 )
Entity ID: 160 ( context_text-Article-91038 )
Entity ID: 164 ( context_text-Article-91068 )
Entity ID: 170 ( context_text-Article-91071 )
Entity ID: 177 ( context_text-Article-91112 )
Entity ID: 186 ( context_text-Article-91114 )
Entity ID: 187 ( context_text-Article-91132 )
Entity ID: 191 ( context_text-Article-91133 )
Entity ID: 202 ( context_text-Article-91157 )
Entity ID: 208 ( context_text-Article-91197 )
Entity ID: 219 ( context_text-Article-91199 )
Entity ID: 223 ( context_text-Article-91254 )
Entity ID: 230 ( context_text-Article-94104 )
Entity ID: 235 ( context_text-Article-94110 )
Entity ID: 244 ( context_text-Article-94125 )
Entity ID: 257 ( context_text-Article-94128 )
Entity ID: 261 ( context_text-Article-94140 )
Entity ID: 265 ( context_text-Article-94301 )
Entity ID: 270 ( context_text-Article-94311 )
Entity ID: 286 ( context_text-Article-94326 )
Entity ID: 297 ( context_text-Article-94405 )
Entity ID: 302 ( context_text-Article-94417 )
Entity ID: 307 ( context_text-Article-94430 )
Entity ID: 312 ( context_text-Article-94442 )

In [93]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | THROUGH
combined_q = from_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO | THROUGH
combined_q = to_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nTO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = from_q | to_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 895 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article)) )

TO - result count: 46 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article)) )

THROUGH - result count: 2743 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article)) )

FROM | TO - result count: 895 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article))) )

FROM | THROUGH - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article))) )

TO | THROUGH - result count: 2789 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article))) )

FROM | TO | THROUGH - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article)) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article)) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" IN (newspaper, article))) )

test AND and OR


In [45]:
# start with all Entity_Relations.
#test_relation_qs = None
test_relation_qs = Entity_Relation.objects.all()

AND-OR - entity type slugs


In [46]:
# try to just find the entity with the desired identifiers.

# two QuerySets, one for just authors, and one for just articles.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# Q for each of article and person entities.
article_entity_q = Q( entity_types__entity_type__slug = "article" )
person_entity_q = Q( entity_types__entity_type__slug = "person" )

# Make a QuerySet for each.
article_entity_qs = entity_qs.filter( article_entity_q )
print( "\narticle entity result count: {} (SQL: {})".format( article_entity_qs.count(), article_entity_qs.query ) )

person_entity_qs = entity_qs.filter( person_entity_q )
print( "\nperson entity result count: {} (SQL: {})".format( person_entity_qs.count(), person_entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

article entity result count: 47 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_types" ON ("context_entity"."id" = "context_entity_types"."entity_id") INNER JOIN "context_entity_type" ON ("context_entity_types"."entity_type_id" = "context_entity_type"."id") WHERE "context_entity_type"."slug" = article)

person entity result count: 268 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_types" ON ("context_entity"."id" = "context_entity_types"."entity_id") INNER JOIN "context_entity_type" ON ("context_entity_types"."entity_type_id" = "context_entity_type"."id") WHERE "context_entity_type"."slug" = person)

In [47]:
relation_qs = Entity_Relation.objects.all()

print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = person_entity_qs )
to_q = Q( relation_to__in = person_entity_qs )
through_q = Q( relation_through__in = article_entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | THROUGH
combined_q = from_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO | THROUGH
combined_q = to_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nTO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = from_q | to_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM & TO & THROUGH
combined_q = from_q & to_q & through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM & TO & THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM & TO & THROUGH - filter
test_qs = relation_qs.filter( from_q )
test_qs = test_qs.filter( to_q )
test_qs = test_qs.filter( through_q )
print( "\nFROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( from_q )
    test_relation_qs = test_relation_qs.filter( to_q )
    test_relation_qs = test_relation_qs.filter( through_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 2320 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) )

TO - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) )

THROUGH - result count: 2743 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article) )

FROM | TO - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person)) )

FROM | THROUGH - result count: 2743 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article)) )

TO | THROUGH - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article)) )

FROM | TO | THROUGH - result count: 3169 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article)) )

FROM & TO & THROUGH - result count: 2320 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article)) )

FROM & TO & THROUGH (filter) - result count: 2320 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article)) )

end-to-end --> FROM & TO & THROUGH (filter) - result count: 2320 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article)) )

AND-OR relation traits

AND-OR relation traits - pub_date

In [40]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )

name_q = Q( entity_relation_trait__name = "pub_date" )
left_range_q = Q( entity_relation_trait__value__gte = "2009-12-01" )
right_range_q = Q( entity_relation_trait__value__lte = "2009-12-31" )
pub_date_q = name_q & left_range_q & right_range_q
test_qs = relation_qs.filter( pub_date_q )
print( "\npub_date result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( pub_date_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

pub_date result count: 1703 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31) )

end-to-end --> FROM & TO & THROUGH (filter) - result count: 1703 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31) )
AND-OR relation traits - sourcenet-coder-User-username

In [41]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

test_value_list = []
test_value_list.append( "automated" )

name_q = Q( entity_relation_trait__name = "sourcenet-coder-User-username" )
value_q = Q( entity_relation_trait__value__in = test_value_list )
coder_username_q = name_q & value_q
relation_qs = relation_qs.filter( coder_username_q )
print( "\ncoder_username result count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( coder_username_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

coder_username result count: 2739 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = sourcenet-coder-User-username AND "context_entity_relation_trait"."value" IN (automated)) )

end-to-end --> FROM & TO & THROUGH (filter) - result count: 1433 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") INNER JOIN "context_entity_relation_trait" T3 ON ("context_entity_relation"."id" = T3."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND T3."name" = sourcenet-coder-User-username AND T3."value" IN (automated)) )
AND-OR relation traits - coder_type

In [50]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

test_value_list = []
test_value_list.append( "OpenCalais_REST_API_v2" )

name_q = Q( entity_relation_trait__name = "coder_type" )
value_q = Q( entity_relation_trait__value__in = test_value_list )
coder_type_q = name_q & value_q
relation_qs = relation_qs.filter( coder_type_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( coder_type_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 2739 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = coder_type AND "context_entity_relation_trait"."value" IN (OpenCalais_REST_API_v2)) )

end-to-end --> FROM & TO & THROUGH (filter) - result count: 1190 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") INNER JOIN "context_entity_relation_trait" T6 ON ("context_entity_relation"."id" = T6."entity_relation_id") INNER JOIN "context_entity_relation_trait" T7 ON ("context_entity_relation"."id" = T7."entity_relation_id") WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article) AND "context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND T6."name" = sourcenet-coder-User-username AND T6."value" IN (automated) AND T7."name" = coder_type AND T7."value" IN (OpenCalais_REST_API_v2)) )
AND-OR relation traits - combined

In [51]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

combined_q = pub_date_q
test_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

combined_q = combined_q & coder_username_q
test_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

combined_q = combined_q & coder_type_q
test_qs = relation_qs.filter( combined_q )
print( "\n\"&\" result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

test_qs = relation_qs.filter( pub_date_q )
test_qs = test_qs.filter( coder_username_q )
test_qs = test_qs.filter( coder_type_q )
print( "\n.filter() result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

combined_q = coder_username_q & coder_type_q
test_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 1703 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31) )

result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND "context_entity_relation_trait"."name" = sourcenet-coder-User-username AND "context_entity_relation_trait"."value" IN (automated)) )

result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND "context_entity_relation_trait"."name" = sourcenet-coder-User-username AND "context_entity_relation_trait"."value" IN (automated) AND "context_entity_relation_trait"."name" = coder_type AND "context_entity_relation_trait"."value" IN (OpenCalais_REST_API_v2)) )

result count: 1433 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") INNER JOIN "context_entity_relation_trait" T3 ON ("context_entity_relation"."id" = T3."entity_relation_id") INNER JOIN "context_entity_relation_trait" T4 ON ("context_entity_relation"."id" = T4."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND T3."name" = sourcenet-coder-User-username AND T3."value" IN (automated) AND T4."name" = coder_type AND T4."value" IN (OpenCalais_REST_API_v2)) )

result count: 0 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = sourcenet-coder-User-username AND "context_entity_relation_trait"."value" IN (automated) AND "context_entity_relation_trait"."name" = coder_type AND "context_entity_relation_trait"."value" IN (OpenCalais_REST_API_v2)) )

In [52]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )


combined_q = pub_date_q | coder_username_q | coder_type_q


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

AND-OR relation type slugs


In [53]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )
test_value_list = []
test_value_list.append( "quoted" )
test_value_list.append( "mentioned" )
test_value_list.append( "shared_byline" )

relation_type_slug_q = Q( relation_type__slug__in = test_value_list )
combined_q = relation_type_slug_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( relation_type_slug_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 437 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") WHERE "context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) )

end-to-end --> FROM & TO & THROUGH (filter) - result count: 248 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") INNER JOIN "context_entity_relation_trait" T6 ON ("context_entity_relation"."id" = T6."entity_relation_id") INNER JOIN "context_entity_relation_trait" T7 ON ("context_entity_relation"."id" = T7."entity_relation_id") INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article) AND "context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND T6."name" = sourcenet-coder-User-username AND T6."value" IN (automated) AND T7."name" = coder_type AND T7."value" IN (OpenCalais_REST_API_v2) AND "context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline)) )

AND-OR entity traits


In [34]:
# try to just find the entity with the desired identifier.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
combined_q = Q( entity_trait__name = "sourcenet-Newspaper-ID" ) & Q( entity_trait__value = "1" )
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 19 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_trait" ON ("context_entity"."id" = "context_entity_trait"."entity_id") WHERE ("context_entity_trait"."name" = sourcenet-Newspaper-ID AND "context_entity_trait"."value" = 1))

In [54]:
debug_flag = False

# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# filter using IDs from above cell
#entity_qs = Entity.objects.filter( pk__in = entity_id_list )

# make Q()s
from_q = Q( relation_from__in = entity_qs )
to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# look for Entity_Relations to an Entity in any relation role (FROM, TO, THROUGH) that match Entity QS

# FROM
test_qs = relation_qs.filter( from_q )
print( "\nFROM - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# TO
test_qs = relation_qs.filter( to_q )
print( "\nTO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# THROUGH
test_qs = relation_qs.filter( through_q )
print( "\nTHROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO
combined_q = from_q | to_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# FROM | TO | THROUGH
combined_q = combined_q | through_q
test_qs = relation_qs.filter( combined_q )
print( "\nFROM | TO | THROUGH - result count: {} ( SQL: {} )".format( test_qs.count(), test_qs.query ) )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( through_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

FROM - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0) )

TO - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0) )

THROUGH - result count: 2743 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0) )

FROM | TO - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0)) )

FROM | TO | THROUGH - result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0) OR "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0) OR "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0)) )

end-to-end --> FROM & TO & THROUGH (filter) - result count: 248 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") INNER JOIN "context_entity_relation_trait" T6 ON ("context_entity_relation"."id" = T6."entity_relation_id") INNER JOIN "context_entity_relation_trait" T7 ON ("context_entity_relation"."id" = T7."entity_relation_id") INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") WHERE ("context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article) AND "context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND T6."name" = sourcenet-coder-User-username AND T6."value" IN (automated) AND T7."name" = coder_type AND T7."value" IN (OpenCalais_REST_API_v2) AND "context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0)) )

test 2-level with multiple aggregated

  • Back to Table of Contents

    {

      "comparison_type": "AND",
      "filter_type": "AND",
      "value_list": [
          {
              "comparison_type": "includes",
              "filter_type": "relation_type_slug",
              "relation_roles_list": [
                  "ALL"
              ],
              "value_list": [
                  "mentioned",
                  "quoted",
                  "shared_byline"
              ]
          },
          {
              "comparison_type": "AND",
              "filter_type": "entity_type_slug",
              "value_list": [
                  {
                      "comparison_type": "equals",
                      "filter_type": "entity_type_slug",
                      "relation_roles_list": [
                          "FROM"
                      ],
                      "value": "person"
                  },
                  {
                      "comparison_type": "equals",
                      "filter_type": "entity_type_slug",
                      "relation_roles_list": [
                          "TO"
                      ],
                      "value": "person"
                  },
                  {
                      "comparison_type": "equals",
                      "filter_type": "entity_type_slug",
                      "relation_roles_list": [
                          "THROUGH"
                      ],
                      "value": "article"
                  }
              ]
          },
          {
              "comparison_type": "AND",
              "filter_type": "relation_trait",
              "value_list": [
                  {
                      "comparison_type": "in_range",
                      "filter_type": "relation_trait",
                      "name": "pub_date",
                      "relation_roles_list": [
                          "ALL"
                      ],
                      "value_from": "2009-12-01",
                      "value_to": "2009-12-31"
                  },
                  {
                      "comparison_type": "includes",
                      "filter_type": "relation_trait",
                      "name": "sourcenet-coder-User-username",
                      "relation_roles_list": [
                          "ALL"
                      ],
                      "value_list": [
                          "automated"
                      ]
                  },
                  {
                      "comparison_type": "includes",
                      "filter_type": "relation_trait",
                      "name": "coder_type",
                      "relation_roles_list": [
                          "ALL"
                      ],
                      "value_list": [
                          "OpenCalais_REST_API_v2"
                      ]
                  }
              ]
          },
          {
              "comparison_type": "includes",
              "filter_type": "entity_trait",
              "name": "sourcenet-Newspaper-ID",
              "relation_roles_list": [
                  "THROUGH"
              ],
              "value_list": [
                  1
              ]
          }
      ]
    

    }


In [71]:
# start with all Entity_Relations.
#test_relation_qs = None
test_relation_qs = Entity_Relation.objects.all()

2-level-multiple - relation type slugs

  • Back to Table of Contents

    {

        "comparison_type": "includes",
        "filter_type": "relation_type_slug",
        "relation_roles_list": [
            "ALL"
        ],
        "value_list": [
            "mentioned",
            "quoted",
            "shared_byline"
        ]
    },

In [72]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

#entity_qs = Entity.objects.filter( pk = 315 )
test_value_list = []
test_value_list.append( "quoted" )
test_value_list.append( "mentioned" )
test_value_list.append( "shared_byline" )

relation_type_slug_q = Q( relation_type__slug__in = test_value_list )
combined_q = relation_type_slug_q
relation_qs = relation_qs.filter( combined_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( relation_type_slug_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 437 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") WHERE "context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) )

end-to-end --> FROM & TO & THROUGH (filter) - result count: 437 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") WHERE "context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) )

2-level-multiple - entity type slugs

  • Back to Table of Contents

    {
        "comparison_type": "AND",
        "filter_type": "entity_type_slug",
        "value_list": [
            {
                "comparison_type": "equals",
                "filter_type": "entity_type_slug",
                "relation_roles_list": [
                    "FROM"
                ],
                "value": "person"
            },
            {
                "comparison_type": "equals",
                "filter_type": "entity_type_slug",
                "relation_roles_list": [
                    "TO"
                ],
                "value": "person"
            },
            {
                "comparison_type": "equals",
                "filter_type": "entity_type_slug",
                "relation_roles_list": [
                    "THROUGH"
                ],
                "value": "article"
            }
        ]
    },

In [73]:
# try to just find the entity with the desired identifiers.

# two QuerySets, one for just authors, and one for just articles.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )

# Q for each of article and person entities.
article_entity_q = Q( entity_types__entity_type__slug = "article" )
person_entity_q = Q( entity_types__entity_type__slug = "person" )

# Make a QuerySet for each.
article_entity_qs = entity_qs.filter( article_entity_q )
print( "\narticle entity result count: {} (SQL: {})".format( article_entity_qs.count(), article_entity_qs.query ) )

person_entity_qs = entity_qs.filter( person_entity_q )
print( "\nperson entity result count: {} (SQL: {})".format( person_entity_qs.count(), person_entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

article entity result count: 47 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_types" ON ("context_entity"."id" = "context_entity_types"."entity_id") INNER JOIN "context_entity_type" ON ("context_entity_types"."entity_type_id" = "context_entity_type"."id") WHERE "context_entity_type"."slug" = article)

person entity result count: 268 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_types" ON ("context_entity"."id" = "context_entity_types"."entity_id") INNER JOIN "context_entity_type" ON ("context_entity_types"."entity_type_id" = "context_entity_type"."id") WHERE "context_entity_type"."slug" = person)

In [74]:
# make Q()s
from_q = Q( relation_from__in = person_entity_qs )
to_q = Q( relation_to__in = person_entity_qs )
through_q = Q( relation_through__in = article_entity_qs )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( from_q )
    test_relation_qs = test_relation_qs.filter( to_q )
    test_relation_qs = test_relation_qs.filter( through_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


end-to-end --> FROM & TO & THROUGH (filter) - result count: 437 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") WHERE ("context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) AND "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article)) )

2-level-multiple - relation traits

  • Back to Table of Contents

    {
        "comparison_type": "AND",
        "filter_type": "relation_trait",
        "value_list": [
            {
                "comparison_type": "in_range",
                "filter_type": "relation_trait",
                "name": "pub_date",
                "relation_roles_list": [
                    "ALL"
                ],
                "value_from": "2009-12-01",
                "value_to": "2009-12-31"
            },
            {
                "comparison_type": "includes",
                "filter_type": "relation_trait",
                "name": "sourcenet-coder-User-username",
                "relation_roles_list": [
                    "ALL"
                ],
                "value_list": [
                    "automated"
                ]
            },
            {
                "comparison_type": "includes",
                "filter_type": "relation_trait",
                "name": "coder_type",
                "relation_roles_list": [
                    "ALL"
                ],
                "value_list": [
                    "OpenCalais_REST_API_v2"
                ]
            }
        ]
    },
2-level-multiple - relation traits - pub_date

In [75]:
name_q = Q( entity_relation_trait__name = "pub_date" )
left_range_q = Q( entity_relation_trait__value__gte = "2009-12-01" )
right_range_q = Q( entity_relation_trait__value__lte = "2009-12-31" )
pub_date_q = name_q & left_range_q & right_range_q

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( pub_date_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


end-to-end --> FROM & TO & THROUGH (filter) - result count: 248 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) AND "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article) AND "context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31) )
2-level-multiple - relation traits - sourcenet-coder-User-username

In [76]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

test_value_list = []
test_value_list.append( "automated" )

name_q = Q( entity_relation_trait__name = "sourcenet-coder-User-username" )
value_q = Q( entity_relation_trait__value__in = test_value_list )
coder_username_q = name_q & value_q
relation_qs = relation_qs.filter( coder_username_q )
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( coder_username_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

result count: 2739 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = sourcenet-coder-User-username AND "context_entity_relation_trait"."value" IN (automated)) )

end-to-end --> FROM & TO & THROUGH (filter) - result count: 248 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") INNER JOIN "context_entity_relation_trait" T7 ON ("context_entity_relation"."id" = T7."entity_relation_id") WHERE ("context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) AND "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article) AND "context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND T7."name" = sourcenet-coder-User-username AND T7."value" IN (automated)) )
2-level-multiple - relation traits - coder_type

In [77]:
# start with Entity_Relation QS
relation_qs = Entity_Relation.objects.all()
print( "\nresult count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

test_value_list = []
test_value_list.append( "OpenCalais_REST_API_v2" )

name_q = Q( entity_relation_trait__name = "coder_type" )
value_q = Q( entity_relation_trait__value__in = test_value_list )
coder_type_q = name_q & value_q
relation_qs = relation_qs.filter( coder_type_q )
print( "\ncoder_type result count: {} ( SQL: {} )".format( relation_qs.count(), relation_qs.query ) )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( coder_type_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


result count: 3215 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" )

coder_type result count: 2739 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") WHERE ("context_entity_relation_trait"."name" = coder_type AND "context_entity_relation_trait"."value" IN (OpenCalais_REST_API_v2)) )

end-to-end --> FROM & TO & THROUGH (filter) - result count: 248 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") INNER JOIN "context_entity_relation_trait" T7 ON ("context_entity_relation"."id" = T7."entity_relation_id") INNER JOIN "context_entity_relation_trait" T8 ON ("context_entity_relation"."id" = T8."entity_relation_id") WHERE ("context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) AND "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article) AND "context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND T7."name" = sourcenet-coder-User-username AND T7."value" IN (automated) AND T8."name" = coder_type AND T8."value" IN (OpenCalais_REST_API_v2)) )

In [78]:
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( pub_date_q )
test_qs = test_qs.filter( coder_username_q )
test_qs = test_qs.filter( coder_type_q )
print( "all 3 together count: {}".format( test_qs.count() ) )


all 3 together count: 1433

2-level-multiple - entity traits

  • Back to Table of Contents

    {
        "comparison_type": "includes",
        "filter_type": "entity_trait",
        "name": "sourcenet-Newspaper-ID",
        "relation_roles_list": [
            "THROUGH"
        ],
        "value_list": [
            1
        ]
    }

In [79]:
# try to just find the entity with the desired identifier.
entity_qs = Entity.objects.all()
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )
combined_q = Q( entity_trait__name = "sourcenet-Newspaper-ID" ) & Q( entity_trait__value = "1" )
entity_qs = entity_qs.filter( combined_q )
print( "\nresult count: {} (SQL: {})".format( entity_qs.count(), entity_qs.query ) )


result count: 319 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity")

result count: 19 (SQL: SELECT "context_entity"."id", "context_entity"."notes", "context_entity"."create_date", "context_entity"."last_modified", "context_entity"."details_json", "context_entity"."name" FROM "context_entity" INNER JOIN "context_entity_trait" ON ("context_entity"."id" = "context_entity_trait"."entity_id") WHERE ("context_entity_trait"."name" = sourcenet-Newspaper-ID AND "context_entity_trait"."value" = 1))

In [80]:
# make Q()s
#from_q = Q( relation_from__in = entity_qs )
#to_q = Q( relation_to__in = entity_qs )
through_q = Q( relation_through__in = entity_qs )

# are we doing an end-to-end test?
if ( test_relation_qs is not None ):

    test_relation_qs = test_relation_qs.filter( through_q )
    print( "\nend-to-end --> FROM & TO & THROUGH (filter) - result count: {} ( SQL: {} )".format( test_relation_qs.count(), test_relation_qs.query ) )
    
#-- END check to see if we are combining these filters. --#


end-to-end --> FROM & TO & THROUGH (filter) - result count: 112 ( SQL: SELECT "context_entity_relation"."id", "context_entity_relation"."notes", "context_entity_relation"."create_date", "context_entity_relation"."last_modified", "context_entity_relation"."details_json", "context_entity_relation"."directed", "context_entity_relation"."relation_from_id", "context_entity_relation"."relation_to_id", "context_entity_relation"."relation_type_id", "context_entity_relation"."relation_through_id" FROM "context_entity_relation" INNER JOIN "context_entity_relation_type" ON ("context_entity_relation"."relation_type_id" = "context_entity_relation_type"."id") INNER JOIN "context_entity_relation_trait" ON ("context_entity_relation"."id" = "context_entity_relation_trait"."entity_relation_id") INNER JOIN "context_entity_relation_trait" T7 ON ("context_entity_relation"."id" = T7."entity_relation_id") INNER JOIN "context_entity_relation_trait" T8 ON ("context_entity_relation"."id" = T8."entity_relation_id") WHERE ("context_entity_relation_type"."slug" IN (quoted, mentioned, shared_byline) AND "context_entity_relation"."relation_from_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_to_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = person) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_types" U1 ON (U0."id" = U1."entity_id") INNER JOIN "context_entity_type" U2 ON (U1."entity_type_id" = U2."id") WHERE U2."slug" = article) AND "context_entity_relation_trait"."name" = pub_date AND "context_entity_relation_trait"."value" >= 2009-12-01 AND "context_entity_relation_trait"."value" <= 2009-12-31 AND T7."name" = sourcenet-coder-User-username AND T7."value" IN (automated) AND T8."name" = coder_type AND T8."value" IN (OpenCalais_REST_API_v2) AND "context_entity_relation"."relation_through_id" IN (SELECT U0."id" FROM "context_entity" U0 INNER JOIN "context_entity_trait" U1 ON (U0."id" = U1."entity_id") WHERE (U1."name" = sourcenet-Newspaper-ID AND U1."value" = 1))) )

from basic file - test 2-level with multiple aggregated


In [66]:
# load basic file into test instance.
test_instance = TestHelper.load_basic()
#test_instance = TestHelper.load_basic_2()

In [67]:
# filter_specification
selection_filters = test_instance.get_selection_filters()
#print( "base_filter_spec: {}".format( base_filter_spec ) )

# retrieve actual "filter_specification".
filter_specification = selection_filters.get( NetworkDataRequest.PROP_NAME_FILTER_SPECIFICATION, None )

# load it into a FilterSpec.
filter_spec = FilterSpec()
filter_spec.set_filter_spec( filter_specification )
print( "filter_spec:\n{}".format( filter_spec.to_json_string() ) )
print( "- comparison type: {}".format( filter_spec.get_comparison_type() ) )


In get_selection_filters(): use_entity_selection_IN is False
filter_spec:
{
    "comparison_type": "AND",
    "filter_type": "AND",
    "value_list": [
        {
            "comparison_type": "includes",
            "filter_type": "relation_type_slug",
            "value_list": [
                "mentioned",
                "quoted",
                "shared_byline"
            ]
        },
        {
            "comparison_type": "AND",
            "filter_type": "relation_trait",
            "value_list": [
                {
                    "comparison_type": "in_range",
                    "data_type": "date",
                    "filter_type": "relation_trait",
                    "name": "pub_date",
                    "value_from": "2009-12-01",
                    "value_to": "2009-12-31"
                },
                {
                    "comparison_type": "includes",
                    "data_type": "string",
                    "filter_type": "relation_trait",
                    "name": "sourcenet-coder-User-username",
                    "value_list": [
                        "automated"
                    ]
                },
                {
                    "comparison_type": "includes",
                    "data_type": "string",
                    "filter_type": "relation_trait",
                    "name": "coder_type",
                    "value_list": [
                        "OpenCalais_REST_API_v2"
                    ]
                }
            ]
        },
        {
            "comparison_type": "AND",
            "filter_type": "entity_type_slug",
            "value_list": [
                {
                    "comparison_type": "includes",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "FROM"
                    ],
                    "value_list": [
                        "person"
                    ]
                },
                {
                    "comparison_type": "includes",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "TO"
                    ],
                    "value_list": [
                        "person"
                    ]
                },
                {
                    "comparison_type": "includes",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "THROUGH"
                    ],
                    "value_list": [
                        "article"
                    ]
                }
            ]
        },
        {
            "comparison_type": "includes",
            "data_type": "int",
            "filter_type": "entity_trait",
            "name": "sourcenet-Newspaper-ID",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value_list": [
                1
            ]
        }
    ]
}
- comparison type: AND

In [68]:
# build out Q() instances
result_status = test_instance.build_filter_spec_q( filter_spec )
print( "build Q() result: {}".format( result_status ) )



In build_filter_spec_q(): comparison type = AND

----> In build_filter_spec_q(): calling aggregate method.
In build_filter_spec_aggregate_q(): comparison type = AND
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'relation_type_slug', 'comparison_type': 'includes', 'value_list': ['mentioned', 'quoted', 'shared_byline']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_relation_type_slug_q; for filter_type: relation_type_slug
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'relation_trait', 'comparison_type': 'AND', 'value_list': [{'filter_type': 'relation_trait', 'name': 'pub_date', 'data_type': 'date', 'comparison_type': 'in_range', 'value_from': '2009-12-01', 'value_to': '2009-12-31'}, {'filter_type': 'relation_trait', 'name': 'sourcenet-coder-User-username', 'data_type': 'string', 'comparison_type': 'includes', 'value_list': ['automated']}, {'filter_type': 'relation_trait', 'name': 'coder_type', 'data_type': 'string', 'comparison_type': 'includes', 'value_list': ['OpenCalais_REST_API_v2']}]}


In build_filter_spec_q(): comparison type = AND

----> In build_filter_spec_q(): calling aggregate method.
In build_filter_spec_aggregate_q(): comparison type = AND
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'relation_trait', 'name': 'pub_date', 'data_type': 'date', 'comparison_type': 'in_range', 'value_from': '2009-12-01', 'value_to': '2009-12-31'}


In build_filter_spec_q(): comparison type = in_range

----> In build_filter_spec_q(): method_name: build_filter_spec_relation_trait_q; for filter_type: relation_trait
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'relation_trait', 'name': 'sourcenet-coder-User-username', 'data_type': 'string', 'comparison_type': 'includes', 'value_list': ['automated']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_relation_trait_q; for filter_type: relation_trait
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'relation_trait', 'name': 'coder_type', 'data_type': 'string', 'comparison_type': 'includes', 'value_list': ['OpenCalais_REST_API_v2']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_relation_trait_q; for filter_type: relation_trait
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'entity_type_slug', 'comparison_type': 'AND', 'value_list': [{'filter_type': 'entity_type_slug', 'comparison_type': 'includes', 'value_list': ['person'], 'relation_roles_list': ['FROM']}, {'filter_type': 'entity_type_slug', 'comparison_type': 'includes', 'value_list': ['person'], 'relation_roles_list': ['TO']}, {'filter_type': 'entity_type_slug', 'comparison_type': 'includes', 'value_list': ['article'], 'relation_roles_list': ['THROUGH']}]}


In build_filter_spec_q(): comparison type = AND

----> In build_filter_spec_q(): calling aggregate method.
In build_filter_spec_aggregate_q(): comparison type = AND
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'entity_type_slug', 'comparison_type': 'includes', 'value_list': ['person'], 'relation_roles_list': ['FROM']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_entity_type_slug_q; for filter_type: entity_type_slug
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'entity_type_slug', 'comparison_type': 'includes', 'value_list': ['person'], 'relation_roles_list': ['TO']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_entity_type_slug_q; for filter_type: entity_type_slug
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'entity_type_slug', 'comparison_type': 'includes', 'value_list': ['article'], 'relation_roles_list': ['THROUGH']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_entity_type_slug_q; for filter_type: entity_type_slug
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'filter_type': 'entity_trait', 'name': 'sourcenet-Newspaper-ID', 'data_type': 'int', 'comparison_type': 'includes', 'value_list': [1], 'relation_roles_list': ['THROUGH']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_entity_trait_q; for filter_type: entity_trait
build Q() result: success: tags = []; messages = []

In [69]:
# try filtering.
relation_qs = Entity_Relation.objects.all()
relation_qs = test_instance.filter_relations_by_filter_spec( relation_qs, filter_spec )
print( "relation_qs count: {}".format( relation_qs.count() ) )



[0] In filter_relations_by_filter_spec():
- filter = <context.export.network.filter_spec.FilterSpec object at 0x7f034cbd7be0>
- level = 0
- filter dict:
{
    "comparison_type": "AND",
    "filter_type": "AND",
    "value_list": [
        {
            "comparison_type": "includes",
            "filter_type": "relation_type_slug",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "mentioned",
                "quoted",
                "shared_byline"
            ]
        },
        {
            "comparison_type": "AND",
            "filter_type": "relation_trait",
            "value_list": [
                {
                    "comparison_type": "in_range",
                    "data_type": "date",
                    "filter_type": "relation_trait",
                    "name": "pub_date",
                    "relation_roles_list": [
                        "ALL"
                    ],
                    "value_from": "2009-12-01",
                    "value_to": "2009-12-31"
                },
                {
                    "comparison_type": "includes",
                    "data_type": "string",
                    "filter_type": "relation_trait",
                    "name": "sourcenet-coder-User-username",
                    "relation_roles_list": [
                        "ALL"
                    ],
                    "value_list": [
                        "automated"
                    ]
                },
                {
                    "comparison_type": "includes",
                    "data_type": "string",
                    "filter_type": "relation_trait",
                    "name": "coder_type",
                    "relation_roles_list": [
                        "ALL"
                    ],
                    "value_list": [
                        "OpenCalais_REST_API_v2"
                    ]
                }
            ]
        },
        {
            "comparison_type": "AND",
            "filter_type": "entity_type_slug",
            "value_list": [
                {
                    "comparison_type": "includes",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "FROM"
                    ],
                    "value_list": [
                        "person"
                    ]
                },
                {
                    "comparison_type": "includes",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "TO"
                    ],
                    "value_list": [
                        "person"
                    ]
                },
                {
                    "comparison_type": "includes",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "THROUGH"
                    ],
                    "value_list": [
                        "article"
                    ]
                }
            ]
        },
        {
            "comparison_type": "includes",
            "data_type": "int",
            "filter_type": "entity_trait",
            "name": "sourcenet-Newspaper-ID",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value_list": [
                1
            ]
        }
    ]
}
- comparison type = AND


[0] ----> In filter_relations_by_filter_spec(): aggregate ( AND ) filter_spec.  Looping over 4 children ( [<context.export.network.filter_spec.FilterSpec object at 0x7f034cbcdeb8>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbcde48>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbcd470>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbcd438>] ).


[0] --------> In filter_relations_by_filter_spec(): child item #1 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "filter_type": "relation_type_slug",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "mentioned",
        "quoted",
        "shared_byline"
    ]
}


[0] --------> In filter_relations_by_filter_spec(): child item #2 - comparison type = AND; JSON:
{
    "comparison_type": "AND",
    "filter_type": "relation_trait",
    "value_list": [
        {
            "comparison_type": "in_range",
            "data_type": "date",
            "filter_type": "relation_trait",
            "name": "pub_date",
            "relation_roles_list": [
                "ALL"
            ],
            "value_from": "2009-12-01",
            "value_to": "2009-12-31"
        },
        {
            "comparison_type": "includes",
            "data_type": "string",
            "filter_type": "relation_trait",
            "name": "sourcenet-coder-User-username",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "automated"
            ]
        },
        {
            "comparison_type": "includes",
            "data_type": "string",
            "filter_type": "relation_trait",
            "name": "coder_type",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "OpenCalais_REST_API_v2"
            ]
        }
    ]
}
In filter_relations_by_filter_spec(): Child is an aggregate comparison type ( AND ).  Parent comparison type: AND  Making recursive call to filter_relations_by_filter_spec() ( filter spec: <context.export.network.filter_spec.FilterSpec object at 0x7f034cbcde48> ).  ( level: 0 ). 


[1] In filter_relations_by_filter_spec():
- filter = <context.export.network.filter_spec.FilterSpec object at 0x7f034cbcde48>
- level = 1
- filter dict:
{
    "comparison_type": "AND",
    "filter_type": "relation_trait",
    "value_list": [
        {
            "comparison_type": "in_range",
            "data_type": "date",
            "filter_type": "relation_trait",
            "name": "pub_date",
            "relation_roles_list": [
                "ALL"
            ],
            "value_from": "2009-12-01",
            "value_to": "2009-12-31"
        },
        {
            "comparison_type": "includes",
            "data_type": "string",
            "filter_type": "relation_trait",
            "name": "sourcenet-coder-User-username",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "automated"
            ]
        },
        {
            "comparison_type": "includes",
            "data_type": "string",
            "filter_type": "relation_trait",
            "name": "coder_type",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "OpenCalais_REST_API_v2"
            ]
        }
    ]
}
- comparison type = AND


[1] ----> In filter_relations_by_filter_spec(): aggregate ( AND ) filter_spec.  Looping over 3 children ( [<context.export.network.filter_spec.FilterSpec object at 0x7f034cbcdf98>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbb7390>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbb7ef0>] ).


[1] --------> In filter_relations_by_filter_spec(): child item #1 - comparison type = in_range; JSON:
{
    "comparison_type": "in_range",
    "data_type": "date",
    "filter_type": "relation_trait",
    "name": "pub_date",
    "relation_roles_list": [
        "ALL"
    ],
    "value_from": "2009-12-01",
    "value_to": "2009-12-31"
}


[1] --------> In filter_relations_by_filter_spec(): child item #2 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "data_type": "string",
    "filter_type": "relation_trait",
    "name": "sourcenet-coder-User-username",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "automated"
    ]
}


[1] --------> In filter_relations_by_filter_spec(): child item #3 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "data_type": "string",
    "filter_type": "relation_trait",
    "name": "coder_type",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "OpenCalais_REST_API_v2"
    ]
}


[0] --------> In filter_relations_by_filter_spec(): child item #3 - comparison type = AND; JSON:
{
    "comparison_type": "AND",
    "filter_type": "entity_type_slug",
    "value_list": [
        {
            "comparison_type": "includes",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "FROM"
            ],
            "value_list": [
                "person"
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "TO"
            ],
            "value_list": [
                "person"
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value_list": [
                "article"
            ]
        }
    ]
}
In filter_relations_by_filter_spec(): Child is an aggregate comparison type ( AND ).  Parent comparison type: AND  Making recursive call to filter_relations_by_filter_spec() ( filter spec: <context.export.network.filter_spec.FilterSpec object at 0x7f034cbcd470> ).  ( level: 0 ). 


[1] In filter_relations_by_filter_spec():
- filter = <context.export.network.filter_spec.FilterSpec object at 0x7f034cbcd470>
- level = 1
- filter dict:
{
    "comparison_type": "AND",
    "filter_type": "entity_type_slug",
    "value_list": [
        {
            "comparison_type": "includes",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "FROM"
            ],
            "value_list": [
                "person"
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "TO"
            ],
            "value_list": [
                "person"
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value_list": [
                "article"
            ]
        }
    ]
}
- comparison type = AND


[1] ----> In filter_relations_by_filter_spec(): aggregate ( AND ) filter_spec.  Looping over 3 children ( [<context.export.network.filter_spec.FilterSpec object at 0x7f034cbf3630>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbd79e8>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbd7eb8>] ).


[1] --------> In filter_relations_by_filter_spec(): child item #1 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "FROM"
    ],
    "value_list": [
        "person"
    ]
}


[1] --------> In filter_relations_by_filter_spec(): child item #2 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "TO"
    ],
    "value_list": [
        "person"
    ]
}


[1] --------> In filter_relations_by_filter_spec(): child item #3 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "THROUGH"
    ],
    "value_list": [
        "article"
    ]
}


[0] --------> In filter_relations_by_filter_spec(): child item #4 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "data_type": "int",
    "filter_type": "entity_trait",
    "name": "sourcenet-Newspaper-ID",
    "relation_roles_list": [
        "THROUGH"
    ],
    "value_list": [
        1
    ]
}
relation_qs count: 112

filter 1 - relation type slug


In [70]:
relation_qs = None
value_list = None
child_filter_spec_list = None
child_filter_spec_count = None
filter_1 = None
filter_1_q = None
test_qs = None
test_count = None
relation_count = None

# init QS
relation_qs = Entity_Relation.objects.all()

# work through child filter spec list
value_list = filter_spec.get_value_list()
child_filter_spec_list = filter_spec.get_child_filter_spec_list()
child_filter_spec_count = len( child_filter_spec_list )
print( "- Child filter spec count: {}".format( child_filter_spec_count ) )

# filter 1
filter_1 = child_filter_spec_list[ 0 ]
print( "Filter 1: {}\nJSON: {}".format( filter_1, filter_1.to_json_string() ) )
value_1 = value_list[ 0 ]
print( "Value 1: {}".format( value_1 ) )

# Q()
filter_1_q = filter_1.get_my_q()
print( "Q 1: {}".format( filter_1_q ) )

# test Q
test_qs = relation_qs.filter( filter_1_q )
test_count = test_qs.count()
print( "----> test_qs count: {}".format( test_count ) )

# update QS
relation_qs = relation_qs.filter( filter_1_q )
relation_count = relation_qs.count()
print( "\n\n- relation_qs count: {}".format( relation_count ) )


- Child filter spec count: 4
Filter 1: <context.export.network.filter_spec.FilterSpec object at 0x7f034cbcdeb8>
JSON: {
    "comparison_type": "includes",
    "filter_type": "relation_type_slug",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "mentioned",
        "quoted",
        "shared_byline"
    ]
}
Value 1: {'filter_type': 'relation_type_slug', 'comparison_type': 'includes', 'value_list': ['mentioned', 'quoted', 'shared_byline'], 'relation_roles_list': ['ALL']}
Q 1: (AND: ('relation_type__slug__in', ['mentioned', 'quoted', 'shared_byline']))
----> test_qs count: 437


- relation_qs count: 437

filter 2 - relation trait


In [71]:
#relation_qs = None
current_q = None
test_qs = None
test_count = None
relation_count = None
filter_2 = None
filter_2_child_list = None
filter_2_child_1 = None
filter_2_child_1_q = None
filter_2_child_2 = None
filter_2_child_2_q = None
filter_2_child_3 = None
filter_2_child_3_q = None

# init QS
#relation_qs = Entity_Relation.objects.all()

# filter 2
filter_2 = child_filter_spec_list[ 1 ]
print( "Filter 2:\n{}".format( filter_2.to_json_string() ) )

# Q()
current_q = filter_2.get_my_q()
print( "Q 1: {}".format( current_q ) )

# children
filter_2_child_list = filter_2.get_child_filter_spec_list()
print( filter_2_child_list )

# child 1
filter_2_child_1 = filter_2_child_list[ 0 ]
print( "\n\nFilter 2, child 1:\n{}".format( filter_2_child_1.to_json_string() ) )

# child 1 Q
filter_2_child_1_q = filter_2_child_1.get_my_q()
print( "Q filter_2_child_1_q: {}".format( filter_2_child_1_q ) )

# test child 1 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_2_child_1_q )
test_count = test_qs.count()
print( "----> test_qs f2c1q count: {}".format( test_count ) )

# child 2
filter_2_child_2 = filter_2_child_list[ 1 ]
print( "\n\nFilter 2, child 2:\n{}".format( filter_2_child_2.to_json_string() ) )

# child 2 Q
filter_2_child_2_q = filter_2_child_2.get_my_q()
print( "Q filter_2_child_2_q: {}".format( filter_2_child_2_q ) )

# test child 2 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_2_child_2_q )
test_count = test_qs.count()
print( "----> test_qs f2c2q count: {}".format( test_count ) )

# child 3
filter_2_child_3 = filter_2_child_list[ 2 ]
print( "\n\nFilter 2, child 3:\n{}".format( filter_2_child_3.to_json_string() ) )

# child 3 Q
filter_2_child_3_q = filter_2_child_3.get_my_q()
print( "Q filter_2_child_3_q: {}".format( filter_2_child_3_q ) )

# test child 3 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_2_child_3_q )
test_count = test_qs.count()
print( "----> test_qs f2c3q count: {}".format( test_count ) )

# test all together
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_2_child_1_q )
test_qs = test_qs.filter( filter_2_child_2_q )
test_qs = test_qs.filter( filter_2_child_3_q )
test_count = test_qs.count()
print( "\n\n- test_qs ALL count: {}".format( test_count ) )

# update QS
relation_qs = relation_qs.filter( filter_2_child_1_q )
relation_qs = relation_qs.filter( filter_2_child_2_q )
relation_qs = relation_qs.filter( filter_2_child_3_q )
relation_count = relation_qs.count()
print( "- relation_qs count: {}".format( relation_count ) )


Filter 2:
{
    "comparison_type": "AND",
    "filter_type": "relation_trait",
    "value_list": [
        {
            "comparison_type": "in_range",
            "data_type": "date",
            "filter_type": "relation_trait",
            "name": "pub_date",
            "relation_roles_list": [
                "ALL"
            ],
            "value_from": "2009-12-01",
            "value_to": "2009-12-31"
        },
        {
            "comparison_type": "includes",
            "data_type": "string",
            "filter_type": "relation_trait",
            "name": "sourcenet-coder-User-username",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "automated"
            ]
        },
        {
            "comparison_type": "includes",
            "data_type": "string",
            "filter_type": "relation_trait",
            "name": "coder_type",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "OpenCalais_REST_API_v2"
            ]
        }
    ]
}
Q 1: None
[<context.export.network.filter_spec.FilterSpec object at 0x7f034cbcdf98>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbb7390>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbb7ef0>]


Filter 2, child 1:
{
    "comparison_type": "in_range",
    "data_type": "date",
    "filter_type": "relation_trait",
    "name": "pub_date",
    "relation_roles_list": [
        "ALL"
    ],
    "value_from": "2009-12-01",
    "value_to": "2009-12-31"
}
Q filter_2_child_1_q: (AND: ('entity_relation_trait__name', 'pub_date'), ('entity_relation_trait__value__gte', '2009-12-01'), ('entity_relation_trait__value__lte', '2009-12-31'))
----> test_qs f2c1q count: 1703


Filter 2, child 2:
{
    "comparison_type": "includes",
    "data_type": "string",
    "filter_type": "relation_trait",
    "name": "sourcenet-coder-User-username",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "automated"
    ]
}
Q filter_2_child_2_q: (AND: ('entity_relation_trait__name', 'sourcenet-coder-User-username'), ('entity_relation_trait__value__in', ['automated']))
----> test_qs f2c2q count: 2739


Filter 2, child 3:
{
    "comparison_type": "includes",
    "data_type": "string",
    "filter_type": "relation_trait",
    "name": "coder_type",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "OpenCalais_REST_API_v2"
    ]
}
Q filter_2_child_3_q: (AND: ('entity_relation_trait__name', 'coder_type'), ('entity_relation_trait__value__in', ['OpenCalais_REST_API_v2']))
----> test_qs f2c3q count: 2739


- test_qs ALL count: 1433
- relation_qs count: 248

filter 3 - entity type slugs


In [72]:
#relation_qs = None
current_q = None
test_qs = None
test_count = None
relation_count = None
filter_3 = None
filter_3_child_list = None
filter_3_child_1 = None
filter_3_child_1_q = None
filter_3_child_2 = None
filter_3_child_2_q = None
filter_3_child_3 = None
filter_3_child_3_q = None

# init QS
#relation_qs = Entity_Relation.objects.all()

# filter 3
filter_3 = child_filter_spec_list[ 2 ]
print( "Filter 3:\n{}".format( filter_3.to_json_string() ) )

# Q()
current_q = filter_3.get_my_q()
print( "Q 1: {}".format( current_q ) )

# children
filter_3_child_list = filter_3.get_child_filter_spec_list()
print( filter_3_child_list )

# child 1
filter_3_child_1 = filter_3_child_list[ 0 ]
print( "\n\nFilter 3, child 1:\n{}".format( filter_3_child_1.to_json_string() ) )

# child 1 Q
filter_3_child_1_q = filter_3_child_1.get_my_q()
print( "Q filter_3_child_1_q: {}".format( filter_3_child_1_q ) )

# test child 1 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_3_child_1_q )
test_count = test_qs.count()
print( "- test_qs f3c1q count: {}".format( test_count ) )

# child 2
filter_3_child_2 = filter_3_child_list[ 1 ]
print( "\n\nFilter 3, child 2:\n{}".format( filter_3_child_2.to_json_string() ) )

# child 2 Q
filter_3_child_2_q = filter_3_child_2.get_my_q()
print( "Q filter_3_child_2_q: {}".format( filter_3_child_2_q ) )

# test child 2 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_3_child_2_q )
test_count = test_qs.count()
print( "- test_qs f3c2q count: {}".format( test_count ) )

# child 3
filter_3_child_3 = filter_3_child_list[ 2 ]
print( "\n\nFilter 3, child 3:\n{}".format( filter_3_child_3.to_json_string() ) )

# child 3 Q
filter_3_child_3_q = filter_3_child_3.get_my_q()
print( "Q filter_3_child_3_q: {}".format( filter_3_child_3_q ) )

# test child 3 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_3_child_3_q )
test_count = test_qs.count()
print( "- test_qs f3c3q count: {}".format( test_count ) )

# test all together
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_3_child_1_q )
test_qs = test_qs.filter( filter_3_child_2_q )
test_qs = test_qs.filter( filter_3_child_3_q )
test_count = test_qs.count()
print( "\n\n- test_qs ALL count: {}".format( test_count ) )

# update QS
relation_qs = relation_qs.filter( filter_3_child_1_q )
relation_qs = relation_qs.filter( filter_3_child_2_q )
relation_qs = relation_qs.filter( filter_3_child_3_q )
relation_count = relation_qs.count()
print( "\n\n- relation_qs count: {}".format( relation_count ) )


Filter 3:
{
    "comparison_type": "AND",
    "filter_type": "entity_type_slug",
    "value_list": [
        {
            "comparison_type": "includes",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "FROM"
            ],
            "value_list": [
                "person"
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "TO"
            ],
            "value_list": [
                "person"
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value_list": [
                "article"
            ]
        }
    ]
}
Q 1: None
[<context.export.network.filter_spec.FilterSpec object at 0x7f034cbf3630>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbd79e8>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbd7eb8>]


Filter 3, child 1:
{
    "comparison_type": "includes",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "FROM"
    ],
    "value_list": [
        "person"
    ]
}
Q filter_3_child_1_q: (AND: ('relation_from__in', <QuerySet [<Entity: 1 - test_entity_1>, <Entity: 2 - test_entity_2>, <Entity: 3 - test_entity_3>, <Entity: 8 - context_text-Person-161>, <Entity: 9 - context_text-Person-46>, <Entity: 10 - context_text-Person-872>, <Entity: 11 - context_text-Person-164>, <Entity: 12 - context_text-Person-165>, <Entity: 13 - context_text-Person-166>, <Entity: 14 - context_text-Person-186>, <Entity: 15 - context_text-Person-162>, <Entity: 16 - context_text-Person-163>, <Entity: 18 - context_text-Person-30>, <Entity: 19 - context_text-Person-750>, <Entity: 20 - context_text-Person-175>, <Entity: 21 - context_text-Person-174>, <Entity: 22 - context_text-Person-176>, <Entity: 23 - context_text-Person-173>, <Entity: 24 - context_text-Person-172>, <Entity: 25 - context_text-Person-32>, '...(remaining elements truncated)...']>))
- test_qs f3c1q count: 2320


Filter 3, child 2:
{
    "comparison_type": "includes",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "TO"
    ],
    "value_list": [
        "person"
    ]
}
Q filter_3_child_2_q: (AND: ('relation_to__in', <QuerySet [<Entity: 1 - test_entity_1>, <Entity: 2 - test_entity_2>, <Entity: 3 - test_entity_3>, <Entity: 8 - context_text-Person-161>, <Entity: 9 - context_text-Person-46>, <Entity: 10 - context_text-Person-872>, <Entity: 11 - context_text-Person-164>, <Entity: 12 - context_text-Person-165>, <Entity: 13 - context_text-Person-166>, <Entity: 14 - context_text-Person-186>, <Entity: 15 - context_text-Person-162>, <Entity: 16 - context_text-Person-163>, <Entity: 18 - context_text-Person-30>, <Entity: 19 - context_text-Person-750>, <Entity: 20 - context_text-Person-175>, <Entity: 21 - context_text-Person-174>, <Entity: 22 - context_text-Person-176>, <Entity: 23 - context_text-Person-173>, <Entity: 24 - context_text-Person-172>, <Entity: 25 - context_text-Person-32>, '...(remaining elements truncated)...']>))
- test_qs f3c2q count: 3169


Filter 3, child 3:
{
    "comparison_type": "includes",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "THROUGH"
    ],
    "value_list": [
        "article"
    ]
}
Q filter_3_child_3_q: (AND: ('relation_through__in', <QuerySet [<Entity: 4 - test_entity_4>, <Entity: 5 - context_text-Article-21409>, <Entity: 17 - context_text-Article-21483>, <Entity: 26 - context_text-Article-21509>, <Entity: 34 - context_text-Article-21512>, <Entity: 37 - context_text-Article-21627>, <Entity: 48 - context_text-Article-21661>, <Entity: 54 - context_text-Article-21719>, <Entity: 62 - context_text-Article-21738>, <Entity: 68 - context_text-Article-21790>, <Entity: 73 - context_text-Article-21827>, <Entity: 79 - context_text-Article-21890>, <Entity: 84 - context_text-Article-21925>, <Entity: 91 - context_text-Article-28274>, <Entity: 98 - context_text-Article-28499>, <Entity: 104 - context_text-Article-28598>, <Entity: 114 - context_text-Article-28610>, <Entity: 116 - context_text-Article-28649>, <Entity: 120 - context_text-Article-28741>, <Entity: 125 - context_text-Article-28846>, '...(remaining elements truncated)...']>))
- test_qs f3c3q count: 2743


- test_qs ALL count: 2320


- relation_qs count: 248

filter 4 - entity trait


In [73]:
filter_4 = None
value_4 = None
filter_4_q = None
test_qs = None
test_count = None
relation_count = None

# filter 4
filter_4 = child_filter_spec_list[ 3 ]
print( "Filter 4: {}\n- JSON: {}".format( filter_4, filter_4.to_json_string() ) )
value_4 = value_list[ 3 ]
print( "Value 4: {}".format( value_4 ) )

# Q()
filter_4_q = filter_4.get_my_q()
print( "Q 1: {}".format( filter_4_q ) )

# test Q
test_qs = relation_qs.filter( filter_4_q )
test_count = test_qs.count()
print( "- test_qs count: {}".format( test_count ) )

# update QS
relation_qs = relation_qs.filter( filter_4_q )
relation_count = relation_qs.count()
print( "- relation_qs count: {}".format( relation_count ) )


Filter 4: <context.export.network.filter_spec.FilterSpec object at 0x7f034cbcd438>
- JSON: {
    "comparison_type": "includes",
    "data_type": "int",
    "filter_type": "entity_trait",
    "name": "sourcenet-Newspaper-ID",
    "relation_roles_list": [
        "THROUGH"
    ],
    "value_list": [
        1
    ]
}
Value 4: {'filter_type': 'entity_trait', 'name': 'sourcenet-Newspaper-ID', 'data_type': 'int', 'comparison_type': 'includes', 'value_list': [1], 'relation_roles_list': ['THROUGH']}
Q 1: (AND: ('relation_through__in', <QuerySet [<Entity: 5 - context_text-Article-21409>, <Entity: 17 - context_text-Article-21483>, <Entity: 26 - context_text-Article-21509>, <Entity: 34 - context_text-Article-21512>, <Entity: 37 - context_text-Article-21627>, <Entity: 48 - context_text-Article-21661>, <Entity: 54 - context_text-Article-21719>, <Entity: 62 - context_text-Article-21738>, <Entity: 68 - context_text-Article-21790>, <Entity: 73 - context_text-Article-21827>, <Entity: 79 - context_text-Article-21890>, <Entity: 84 - context_text-Article-21925>, <Entity: 91 - context_text-Article-28274>, <Entity: 98 - context_text-Article-28499>, <Entity: 104 - context_text-Article-28598>, <Entity: 114 - context_text-Article-28610>, <Entity: 116 - context_text-Article-28649>, <Entity: 120 - context_text-Article-28741>, <Entity: 125 - context_text-Article-28846>]>))
- test_qs count: 112
- relation_qs count: 112

from basic 2 file - test 2-level with multiple aggregated


In [74]:
# load basic file into test instance.
#test_instance = TestHelper.load_basic()
test_instance = TestHelper.load_basic_2()

In [75]:
# filter_specification
selection_filters = test_instance.get_selection_filters()
#print( "base_filter_spec: {}".format( base_filter_spec ) )

# retrieve actual "filter_specification".
filter_specification = selection_filters.get( NetworkDataRequest.PROP_NAME_FILTER_SPECIFICATION, None )

# load it into a FilterSpec.
filter_spec = FilterSpec()
filter_spec.set_filter_spec( filter_specification )
print( "filter_spec:\n{}".format( filter_spec.to_json_string() ) )
print( "- comparison type: {}".format( filter_spec.get_comparison_type() ) )


In get_selection_filters(): use_entity_selection_IN is False
filter_spec:
{
    "comparison_type": "AND",
    "filter_type": "AND",
    "value_list": [
        {
            "comparison_type": "includes",
            "filter_type": "relation_type_slug",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "mentioned",
                "quoted",
                "shared_byline"
            ]
        },
        {
            "comparison_type": "AND",
            "filter_type": "entity_type_slug",
            "value_list": [
                {
                    "comparison_type": "equals",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "FROM"
                    ],
                    "value": "person"
                },
                {
                    "comparison_type": "equals",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "TO"
                    ],
                    "value": "person"
                },
                {
                    "comparison_type": "equals",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "THROUGH"
                    ],
                    "value": "article"
                }
            ]
        },
        {
            "comparison_type": "AND",
            "filter_type": "relation_trait",
            "value_list": [
                {
                    "comparison_type": "in_range",
                    "filter_type": "relation_trait",
                    "name": "pub_date",
                    "relation_roles_list": [
                        "ALL"
                    ],
                    "value_from": "2009-12-01",
                    "value_to": "2009-12-31"
                },
                {
                    "comparison_type": "includes",
                    "filter_type": "relation_trait",
                    "name": "sourcenet-coder-User-username",
                    "relation_roles_list": [
                        "ALL"
                    ],
                    "value_list": [
                        "automated"
                    ]
                },
                {
                    "comparison_type": "includes",
                    "filter_type": "relation_trait",
                    "name": "coder_type",
                    "relation_roles_list": [
                        "ALL"
                    ],
                    "value_list": [
                        "OpenCalais_REST_API_v2"
                    ]
                }
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "entity_trait",
            "name": "sourcenet-Newspaper-ID",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value_list": [
                1
            ]
        }
    ]
}
- comparison type: AND

In [76]:
# build out Q() instances
result_status = test_instance.build_filter_spec_q( filter_spec )
print( "build Q() result: {}".format( result_status ) )



In build_filter_spec_q(): comparison type = AND

----> In build_filter_spec_q(): calling aggregate method.
In build_filter_spec_aggregate_q(): comparison type = AND
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'includes', 'filter_type': 'relation_type_slug', 'relation_roles_list': ['ALL'], 'value_list': ['mentioned', 'quoted', 'shared_byline']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_relation_type_slug_q; for filter_type: relation_type_slug
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'AND', 'filter_type': 'entity_type_slug', 'value_list': [{'comparison_type': 'equals', 'filter_type': 'entity_type_slug', 'relation_roles_list': ['FROM'], 'value': 'person'}, {'comparison_type': 'equals', 'filter_type': 'entity_type_slug', 'relation_roles_list': ['TO'], 'value': 'person'}, {'comparison_type': 'equals', 'filter_type': 'entity_type_slug', 'relation_roles_list': ['THROUGH'], 'value': 'article'}]}


In build_filter_spec_q(): comparison type = AND

----> In build_filter_spec_q(): calling aggregate method.
In build_filter_spec_aggregate_q(): comparison type = AND
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'equals', 'filter_type': 'entity_type_slug', 'relation_roles_list': ['FROM'], 'value': 'person'}


In build_filter_spec_q(): comparison type = equals

----> In build_filter_spec_q(): method_name: build_filter_spec_entity_type_slug_q; for filter_type: entity_type_slug
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'equals', 'filter_type': 'entity_type_slug', 'relation_roles_list': ['TO'], 'value': 'person'}


In build_filter_spec_q(): comparison type = equals

----> In build_filter_spec_q(): method_name: build_filter_spec_entity_type_slug_q; for filter_type: entity_type_slug
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'equals', 'filter_type': 'entity_type_slug', 'relation_roles_list': ['THROUGH'], 'value': 'article'}


In build_filter_spec_q(): comparison type = equals

----> In build_filter_spec_q(): method_name: build_filter_spec_entity_type_slug_q; for filter_type: entity_type_slug
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'AND', 'filter_type': 'relation_trait', 'value_list': [{'comparison_type': 'in_range', 'filter_type': 'relation_trait', 'name': 'pub_date', 'relation_roles_list': ['ALL'], 'value_from': '2009-12-01', 'value_to': '2009-12-31'}, {'comparison_type': 'includes', 'filter_type': 'relation_trait', 'name': 'sourcenet-coder-User-username', 'relation_roles_list': ['ALL'], 'value_list': ['automated']}, {'comparison_type': 'includes', 'filter_type': 'relation_trait', 'name': 'coder_type', 'relation_roles_list': ['ALL'], 'value_list': ['OpenCalais_REST_API_v2']}]}


In build_filter_spec_q(): comparison type = AND

----> In build_filter_spec_q(): calling aggregate method.
In build_filter_spec_aggregate_q(): comparison type = AND
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'in_range', 'filter_type': 'relation_trait', 'name': 'pub_date', 'relation_roles_list': ['ALL'], 'value_from': '2009-12-01', 'value_to': '2009-12-31'}


In build_filter_spec_q(): comparison type = in_range

----> In build_filter_spec_q(): method_name: build_filter_spec_relation_trait_q; for filter_type: relation_trait
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'includes', 'filter_type': 'relation_trait', 'name': 'sourcenet-coder-User-username', 'relation_roles_list': ['ALL'], 'value_list': ['automated']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_relation_trait_q; for filter_type: relation_trait
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'includes', 'filter_type': 'relation_trait', 'name': 'coder_type', 'relation_roles_list': ['ALL'], 'value_list': ['OpenCalais_REST_API_v2']}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_relation_trait_q; for filter_type: relation_trait
In build_filter_spec_aggregate_q(): looping over nested spec JSON, current JSON: {'comparison_type': 'includes', 'filter_type': 'entity_trait', 'name': 'sourcenet-Newspaper-ID', 'relation_roles_list': ['THROUGH'], 'value_list': [1]}


In build_filter_spec_q(): comparison type = includes

----> In build_filter_spec_q(): method_name: build_filter_spec_entity_trait_q; for filter_type: entity_trait
build Q() result: success: tags = []; messages = []

In [77]:
# try filtering.
relation_qs = Entity_Relation.objects.all()
relation_qs = test_instance.filter_relations_by_filter_spec( relation_qs, filter_spec )
print( "relation_qs count: {}".format( relation_qs.count() ) )



[0] In filter_relations_by_filter_spec():
- filter = <context.export.network.filter_spec.FilterSpec object at 0x7f034cbdbc18>
- level = 0
- filter dict:
{
    "comparison_type": "AND",
    "filter_type": "AND",
    "value_list": [
        {
            "comparison_type": "includes",
            "filter_type": "relation_type_slug",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "mentioned",
                "quoted",
                "shared_byline"
            ]
        },
        {
            "comparison_type": "AND",
            "filter_type": "entity_type_slug",
            "value_list": [
                {
                    "comparison_type": "equals",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "FROM"
                    ],
                    "value": "person"
                },
                {
                    "comparison_type": "equals",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "TO"
                    ],
                    "value": "person"
                },
                {
                    "comparison_type": "equals",
                    "filter_type": "entity_type_slug",
                    "relation_roles_list": [
                        "THROUGH"
                    ],
                    "value": "article"
                }
            ]
        },
        {
            "comparison_type": "AND",
            "filter_type": "relation_trait",
            "value_list": [
                {
                    "comparison_type": "in_range",
                    "filter_type": "relation_trait",
                    "name": "pub_date",
                    "relation_roles_list": [
                        "ALL"
                    ],
                    "value_from": "2009-12-01",
                    "value_to": "2009-12-31"
                },
                {
                    "comparison_type": "includes",
                    "filter_type": "relation_trait",
                    "name": "sourcenet-coder-User-username",
                    "relation_roles_list": [
                        "ALL"
                    ],
                    "value_list": [
                        "automated"
                    ]
                },
                {
                    "comparison_type": "includes",
                    "filter_type": "relation_trait",
                    "name": "coder_type",
                    "relation_roles_list": [
                        "ALL"
                    ],
                    "value_list": [
                        "OpenCalais_REST_API_v2"
                    ]
                }
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "entity_trait",
            "name": "sourcenet-Newspaper-ID",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value_list": [
                1
            ]
        }
    ]
}
- comparison type = AND


[0] ----> In filter_relations_by_filter_spec(): aggregate ( AND ) filter_spec.  Looping over 4 children ( [<context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8ba8>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8278>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8cf8>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8b00>] ).


[0] --------> In filter_relations_by_filter_spec(): child item #1 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "filter_type": "relation_type_slug",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "mentioned",
        "quoted",
        "shared_byline"
    ]
}


[0] --------> In filter_relations_by_filter_spec(): child item #2 - comparison type = AND; JSON:
{
    "comparison_type": "AND",
    "filter_type": "entity_type_slug",
    "value_list": [
        {
            "comparison_type": "equals",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "FROM"
            ],
            "value": "person"
        },
        {
            "comparison_type": "equals",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "TO"
            ],
            "value": "person"
        },
        {
            "comparison_type": "equals",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value": "article"
        }
    ]
}
In filter_relations_by_filter_spec(): Child is an aggregate comparison type ( AND ).  Parent comparison type: AND  Making recursive call to filter_relations_by_filter_spec() ( filter spec: <context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8278> ).  ( level: 0 ). 


[1] In filter_relations_by_filter_spec():
- filter = <context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8278>
- level = 1
- filter dict:
{
    "comparison_type": "AND",
    "filter_type": "entity_type_slug",
    "value_list": [
        {
            "comparison_type": "equals",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "FROM"
            ],
            "value": "person"
        },
        {
            "comparison_type": "equals",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "TO"
            ],
            "value": "person"
        },
        {
            "comparison_type": "equals",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value": "article"
        }
    ]
}
- comparison type = AND


[1] ----> In filter_relations_by_filter_spec(): aggregate ( AND ) filter_spec.  Looping over 3 children ( [<context.export.network.filter_spec.FilterSpec object at 0x7f034cbd7d68>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbdbf60>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbdb6a0>] ).


[1] --------> In filter_relations_by_filter_spec(): child item #1 - comparison type = equals; JSON:
{
    "comparison_type": "equals",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "FROM"
    ],
    "value": "person"
}


[1] --------> In filter_relations_by_filter_spec(): child item #2 - comparison type = equals; JSON:
{
    "comparison_type": "equals",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "TO"
    ],
    "value": "person"
}


[1] --------> In filter_relations_by_filter_spec(): child item #3 - comparison type = equals; JSON:
{
    "comparison_type": "equals",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "THROUGH"
    ],
    "value": "article"
}


[0] --------> In filter_relations_by_filter_spec(): child item #3 - comparison type = AND; JSON:
{
    "comparison_type": "AND",
    "filter_type": "relation_trait",
    "value_list": [
        {
            "comparison_type": "in_range",
            "filter_type": "relation_trait",
            "name": "pub_date",
            "relation_roles_list": [
                "ALL"
            ],
            "value_from": "2009-12-01",
            "value_to": "2009-12-31"
        },
        {
            "comparison_type": "includes",
            "filter_type": "relation_trait",
            "name": "sourcenet-coder-User-username",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "automated"
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "relation_trait",
            "name": "coder_type",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "OpenCalais_REST_API_v2"
            ]
        }
    ]
}
In filter_relations_by_filter_spec(): Child is an aggregate comparison type ( AND ).  Parent comparison type: AND  Making recursive call to filter_relations_by_filter_spec() ( filter spec: <context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8cf8> ).  ( level: 0 ). 


[1] In filter_relations_by_filter_spec():
- filter = <context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8cf8>
- level = 1
- filter dict:
{
    "comparison_type": "AND",
    "filter_type": "relation_trait",
    "value_list": [
        {
            "comparison_type": "in_range",
            "filter_type": "relation_trait",
            "name": "pub_date",
            "relation_roles_list": [
                "ALL"
            ],
            "value_from": "2009-12-01",
            "value_to": "2009-12-31"
        },
        {
            "comparison_type": "includes",
            "filter_type": "relation_trait",
            "name": "sourcenet-coder-User-username",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "automated"
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "relation_trait",
            "name": "coder_type",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "OpenCalais_REST_API_v2"
            ]
        }
    ]
}
- comparison type = AND


[1] ----> In filter_relations_by_filter_spec(): aggregate ( AND ) filter_spec.  Looping over 3 children ( [<context.export.network.filter_spec.FilterSpec object at 0x7f034cbdbba8>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbabc50>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cba7d30>] ).


[1] --------> In filter_relations_by_filter_spec(): child item #1 - comparison type = in_range; JSON:
{
    "comparison_type": "in_range",
    "filter_type": "relation_trait",
    "name": "pub_date",
    "relation_roles_list": [
        "ALL"
    ],
    "value_from": "2009-12-01",
    "value_to": "2009-12-31"
}


[1] --------> In filter_relations_by_filter_spec(): child item #2 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "filter_type": "relation_trait",
    "name": "sourcenet-coder-User-username",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "automated"
    ]
}


[1] --------> In filter_relations_by_filter_spec(): child item #3 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "filter_type": "relation_trait",
    "name": "coder_type",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "OpenCalais_REST_API_v2"
    ]
}


[0] --------> In filter_relations_by_filter_spec(): child item #4 - comparison type = includes; JSON:
{
    "comparison_type": "includes",
    "filter_type": "entity_trait",
    "name": "sourcenet-Newspaper-ID",
    "relation_roles_list": [
        "THROUGH"
    ],
    "value_list": [
        1
    ]
}
relation_qs count: 112

filter 1 - relation type slug


In [78]:
relation_qs = None
value_list = None
child_filter_spec_list = None
child_filter_spec_count = None
filter_1 = None
filter_1_q = None
test_qs = None
test_count = None
relation_count = None

# init QS
relation_qs = Entity_Relation.objects.all()

# work through child filter spec list
value_list = filter_spec.get_value_list()
child_filter_spec_list = filter_spec.get_child_filter_spec_list()
child_filter_spec_count = len( child_filter_spec_list )
print( "- Child filter spec count: {}".format( child_filter_spec_count ) )

# filter 1
filter_1 = child_filter_spec_list[ 0 ]
print( "Filter 1: {}\nJSON: {}".format( filter_1, filter_1.to_json_string() ) )
value_1 = value_list[ 0 ]
print( "Value 1: {}".format( value_1 ) )

# Q()
filter_1_q = filter_1.get_my_q()
print( "Q 1: {}".format( filter_1_q ) )

# test Q
test_qs = relation_qs.filter( filter_1_q )
test_count = test_qs.count()
print( "----> test_qs count: {}".format( test_count ) )

# update QS
relation_qs = relation_qs.filter( filter_1_q )
relation_count = relation_qs.count()
print( "\n\n- relation_qs count: {}".format( relation_count ) )


- Child filter spec count: 4
Filter 1: <context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8ba8>
JSON: {
    "comparison_type": "includes",
    "filter_type": "relation_type_slug",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "mentioned",
        "quoted",
        "shared_byline"
    ]
}
Value 1: {'comparison_type': 'includes', 'filter_type': 'relation_type_slug', 'relation_roles_list': ['ALL'], 'value_list': ['mentioned', 'quoted', 'shared_byline']}
Q 1: (AND: ('relation_type__slug__in', ['mentioned', 'quoted', 'shared_byline']))
----> test_qs count: 437


- relation_qs count: 437

filter 2 - entity type slug


In [79]:
#relation_qs = None
current_q = None
test_qs = None
test_count = None
relation_count = None
filter_2 = None
filter_2_child_list = None
filter_2_child_1 = None
filter_2_child_1_q = None
filter_2_child_2 = None
filter_2_child_2_q = None
filter_2_child_3 = None
filter_2_child_3_q = None

# init QS
#relation_qs = Entity_Relation.objects.all()

# filter 2
filter_2 = child_filter_spec_list[ 1 ]
print( "Filter 2:\n{}".format( filter_2.to_json_string() ) )

# Q()
current_q = filter_2.get_my_q()
print( "Q 1: {}".format( current_q ) )

# children
filter_2_child_list = filter_2.get_child_filter_spec_list()
print( filter_2_child_list )

# child 1
filter_2_child_1 = filter_2_child_list[ 0 ]
print( "\n\nFilter 2, child 1:\n{}".format( filter_2_child_1.to_json_string() ) )

# child 1 Q
filter_2_child_1_q = filter_2_child_1.get_my_q()
print( "Q filter_2_child_1_q: {}".format( filter_2_child_1_q ) )

# test child 1 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_2_child_1_q )
test_count = test_qs.count()
print( "----> test_qs f2c1q count: {}".format( test_count ) )

# child 2
filter_2_child_2 = filter_2_child_list[ 1 ]
print( "\n\nFilter 2, child 2:\n{}".format( filter_2_child_2.to_json_string() ) )

# child 2 Q
filter_2_child_2_q = filter_2_child_2.get_my_q()
print( "Q filter_2_child_2_q: {}".format( filter_2_child_2_q ) )

# test child 2 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_2_child_2_q )
test_count = test_qs.count()
print( "----> test_qs f2c2q count: {}".format( test_count ) )

# child 3
filter_2_child_3 = filter_2_child_list[ 2 ]
print( "\n\nFilter 2, child 3:\n{}".format( filter_2_child_3.to_json_string() ) )

# child 3 Q
filter_2_child_3_q = filter_2_child_3.get_my_q()
print( "Q filter_2_child_3_q: {}".format( filter_2_child_3_q ) )

# test child 3 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_2_child_3_q )
test_count = test_qs.count()
print( "----> test_qs f2c3q count: {}".format( test_count ) )

# test all together
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_2_child_1_q )
test_qs = test_qs.filter( filter_2_child_2_q )
test_qs = test_qs.filter( filter_2_child_3_q )
test_count = test_qs.count()
print( "\n\n- test_qs ALL count: {}".format( test_count ) )

# update QS
relation_qs = relation_qs.filter( filter_2_child_1_q )
relation_qs = relation_qs.filter( filter_2_child_2_q )
relation_qs = relation_qs.filter( filter_2_child_3_q )
relation_count = relation_qs.count()
print( "- relation_qs count: {}".format( relation_count ) )


Filter 2:
{
    "comparison_type": "AND",
    "filter_type": "entity_type_slug",
    "value_list": [
        {
            "comparison_type": "equals",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "FROM"
            ],
            "value": "person"
        },
        {
            "comparison_type": "equals",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "TO"
            ],
            "value": "person"
        },
        {
            "comparison_type": "equals",
            "filter_type": "entity_type_slug",
            "relation_roles_list": [
                "THROUGH"
            ],
            "value": "article"
        }
    ]
}
Q 1: None
[<context.export.network.filter_spec.FilterSpec object at 0x7f034cbd7d68>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbdbf60>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbdb6a0>]


Filter 2, child 1:
{
    "comparison_type": "equals",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "FROM"
    ],
    "value": "person"
}
Q filter_2_child_1_q: (AND: ('relation_from__in', <QuerySet [<Entity: 1 - test_entity_1>, <Entity: 2 - test_entity_2>, <Entity: 3 - test_entity_3>, <Entity: 8 - context_text-Person-161>, <Entity: 9 - context_text-Person-46>, <Entity: 10 - context_text-Person-872>, <Entity: 11 - context_text-Person-164>, <Entity: 12 - context_text-Person-165>, <Entity: 13 - context_text-Person-166>, <Entity: 14 - context_text-Person-186>, <Entity: 15 - context_text-Person-162>, <Entity: 16 - context_text-Person-163>, <Entity: 18 - context_text-Person-30>, <Entity: 19 - context_text-Person-750>, <Entity: 20 - context_text-Person-175>, <Entity: 21 - context_text-Person-174>, <Entity: 22 - context_text-Person-176>, <Entity: 23 - context_text-Person-173>, <Entity: 24 - context_text-Person-172>, <Entity: 25 - context_text-Person-32>, '...(remaining elements truncated)...']>))
----> test_qs f2c1q count: 2320


Filter 2, child 2:
{
    "comparison_type": "equals",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "TO"
    ],
    "value": "person"
}
Q filter_2_child_2_q: (AND: ('relation_to__in', <QuerySet [<Entity: 1 - test_entity_1>, <Entity: 2 - test_entity_2>, <Entity: 3 - test_entity_3>, <Entity: 8 - context_text-Person-161>, <Entity: 9 - context_text-Person-46>, <Entity: 10 - context_text-Person-872>, <Entity: 11 - context_text-Person-164>, <Entity: 12 - context_text-Person-165>, <Entity: 13 - context_text-Person-166>, <Entity: 14 - context_text-Person-186>, <Entity: 15 - context_text-Person-162>, <Entity: 16 - context_text-Person-163>, <Entity: 18 - context_text-Person-30>, <Entity: 19 - context_text-Person-750>, <Entity: 20 - context_text-Person-175>, <Entity: 21 - context_text-Person-174>, <Entity: 22 - context_text-Person-176>, <Entity: 23 - context_text-Person-173>, <Entity: 24 - context_text-Person-172>, <Entity: 25 - context_text-Person-32>, '...(remaining elements truncated)...']>))
----> test_qs f2c2q count: 3169


Filter 2, child 3:
{
    "comparison_type": "equals",
    "filter_type": "entity_type_slug",
    "relation_roles_list": [
        "THROUGH"
    ],
    "value": "article"
}
Q filter_2_child_3_q: (AND: ('relation_through__in', <QuerySet [<Entity: 4 - test_entity_4>, <Entity: 5 - context_text-Article-21409>, <Entity: 17 - context_text-Article-21483>, <Entity: 26 - context_text-Article-21509>, <Entity: 34 - context_text-Article-21512>, <Entity: 37 - context_text-Article-21627>, <Entity: 48 - context_text-Article-21661>, <Entity: 54 - context_text-Article-21719>, <Entity: 62 - context_text-Article-21738>, <Entity: 68 - context_text-Article-21790>, <Entity: 73 - context_text-Article-21827>, <Entity: 79 - context_text-Article-21890>, <Entity: 84 - context_text-Article-21925>, <Entity: 91 - context_text-Article-28274>, <Entity: 98 - context_text-Article-28499>, <Entity: 104 - context_text-Article-28598>, <Entity: 114 - context_text-Article-28610>, <Entity: 116 - context_text-Article-28649>, <Entity: 120 - context_text-Article-28741>, <Entity: 125 - context_text-Article-28846>, '...(remaining elements truncated)...']>))
----> test_qs f2c3q count: 2743


- test_qs ALL count: 2320
- relation_qs count: 437

filter 3 - relation trait


In [80]:
#relation_qs = None
current_q = None
test_qs = None
test_count = None
relation_count = None
filter_3 = None
filter_3_child_list = None
filter_3_child_1 = None
filter_3_child_1_q = None
filter_3_child_2 = None
filter_3_child_2_q = None
filter_3_child_3 = None
filter_3_child_3_q = None

# init QS
#relation_qs = Entity_Relation.objects.all()

# filter 3
filter_3 = child_filter_spec_list[ 2 ]
print( "Filter 3:\n{}".format( filter_3.to_json_string() ) )

# Q()
current_q = filter_3.get_my_q()
print( "Q 1: {}".format( current_q ) )

# children
filter_3_child_list = filter_3.get_child_filter_spec_list()
print( filter_3_child_list )

# child 1
filter_3_child_1 = filter_3_child_list[ 0 ]
print( "\n\nFilter 3, child 1:\n{}".format( filter_3_child_1.to_json_string() ) )

# child 1 Q
filter_3_child_1_q = filter_3_child_1.get_my_q()
print( "Q filter_3_child_1_q: {}".format( filter_3_child_1_q ) )

# test child 1 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_3_child_1_q )
test_count = test_qs.count()
print( "- test_qs f3c1q count: {}".format( test_count ) )

# child 2
filter_3_child_2 = filter_3_child_list[ 1 ]
print( "\n\nFilter 3, child 2:\n{}".format( filter_3_child_2.to_json_string() ) )

# child 2 Q
filter_3_child_2_q = filter_3_child_2.get_my_q()
print( "Q filter_3_child_2_q: {}".format( filter_3_child_2_q ) )

# test child 2 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_3_child_2_q )
test_count = test_qs.count()
print( "- test_qs f3c2q count: {}".format( test_count ) )

# child 3
filter_3_child_3 = filter_3_child_list[ 2 ]
print( "\n\nFilter 3, child 3:\n{}".format( filter_3_child_3.to_json_string() ) )

# child 3 Q
filter_3_child_3_q = filter_3_child_3.get_my_q()
print( "Q filter_3_child_3_q: {}".format( filter_3_child_3_q ) )

# test child 3 Q
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_3_child_3_q )
test_count = test_qs.count()
print( "- test_qs f3c3q count: {}".format( test_count ) )

# test all together
test_qs = Entity_Relation.objects.all()
test_qs = test_qs.filter( filter_3_child_1_q )
test_qs = test_qs.filter( filter_3_child_2_q )
test_qs = test_qs.filter( filter_3_child_3_q )
test_count = test_qs.count()
print( "\n\n- test_qs ALL count: {}".format( test_count ) )

# update QS
relation_qs = relation_qs.filter( filter_3_child_1_q )
relation_qs = relation_qs.filter( filter_3_child_2_q )
relation_qs = relation_qs.filter( filter_3_child_3_q )
relation_count = relation_qs.count()
print( "\n\n- relation_qs count: {}".format( relation_count ) )


Filter 3:
{
    "comparison_type": "AND",
    "filter_type": "relation_trait",
    "value_list": [
        {
            "comparison_type": "in_range",
            "filter_type": "relation_trait",
            "name": "pub_date",
            "relation_roles_list": [
                "ALL"
            ],
            "value_from": "2009-12-01",
            "value_to": "2009-12-31"
        },
        {
            "comparison_type": "includes",
            "filter_type": "relation_trait",
            "name": "sourcenet-coder-User-username",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "automated"
            ]
        },
        {
            "comparison_type": "includes",
            "filter_type": "relation_trait",
            "name": "coder_type",
            "relation_roles_list": [
                "ALL"
            ],
            "value_list": [
                "OpenCalais_REST_API_v2"
            ]
        }
    ]
}
Q 1: None
[<context.export.network.filter_spec.FilterSpec object at 0x7f034cbdbba8>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cbabc50>, <context.export.network.filter_spec.FilterSpec object at 0x7f034cba7d30>]


Filter 3, child 1:
{
    "comparison_type": "in_range",
    "filter_type": "relation_trait",
    "name": "pub_date",
    "relation_roles_list": [
        "ALL"
    ],
    "value_from": "2009-12-01",
    "value_to": "2009-12-31"
}
Q filter_3_child_1_q: (AND: ('entity_relation_trait__name', 'pub_date'), ('entity_relation_trait__value__gte', '2009-12-01'), ('entity_relation_trait__value__lte', '2009-12-31'))
- test_qs f3c1q count: 1703


Filter 3, child 2:
{
    "comparison_type": "includes",
    "filter_type": "relation_trait",
    "name": "sourcenet-coder-User-username",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "automated"
    ]
}
Q filter_3_child_2_q: (AND: ('entity_relation_trait__name', 'sourcenet-coder-User-username'), ('entity_relation_trait__value__in', ['automated']))
- test_qs f3c2q count: 2739


Filter 3, child 3:
{
    "comparison_type": "includes",
    "filter_type": "relation_trait",
    "name": "coder_type",
    "relation_roles_list": [
        "ALL"
    ],
    "value_list": [
        "OpenCalais_REST_API_v2"
    ]
}
Q filter_3_child_3_q: (AND: ('entity_relation_trait__name', 'coder_type'), ('entity_relation_trait__value__in', ['OpenCalais_REST_API_v2']))
- test_qs f3c3q count: 2739


- test_qs ALL count: 1433


- relation_qs count: 248

filter 4 - entity trait


In [81]:
filter_4 = None
value_4 = None
filter_4_q = None
test_qs = None
test_count = None
relation_count = None

# filter 4
filter_4 = child_filter_spec_list[ 3 ]
print( "Filter 4: {}\n- JSON: {}".format( filter_4, filter_4.to_json_string() ) )
value_4 = value_list[ 3 ]
print( "Value 4: {}".format( value_4 ) )

# Q()
filter_4_q = filter_4.get_my_q()
print( "Q 1: {}".format( filter_4_q ) )

# test Q
test_qs = relation_qs.filter( filter_4_q )
test_count = test_qs.count()
print( "- test_qs count: {}".format( test_count ) )

# update QS
relation_qs = relation_qs.filter( filter_4_q )
relation_count = relation_qs.count()
print( "- relation_qs count: {}".format( relation_count ) )


Filter 4: <context.export.network.filter_spec.FilterSpec object at 0x7f034cbc8b00>
- JSON: {
    "comparison_type": "includes",
    "filter_type": "entity_trait",
    "name": "sourcenet-Newspaper-ID",
    "relation_roles_list": [
        "THROUGH"
    ],
    "value_list": [
        1
    ]
}
Value 4: {'comparison_type': 'includes', 'filter_type': 'entity_trait', 'name': 'sourcenet-Newspaper-ID', 'relation_roles_list': ['THROUGH'], 'value_list': [1]}
Q 1: (AND: ('relation_through__in', <QuerySet [<Entity: 5 - context_text-Article-21409>, <Entity: 17 - context_text-Article-21483>, <Entity: 26 - context_text-Article-21509>, <Entity: 34 - context_text-Article-21512>, <Entity: 37 - context_text-Article-21627>, <Entity: 48 - context_text-Article-21661>, <Entity: 54 - context_text-Article-21719>, <Entity: 62 - context_text-Article-21738>, <Entity: 68 - context_text-Article-21790>, <Entity: 73 - context_text-Article-21827>, <Entity: 79 - context_text-Article-21890>, <Entity: 84 - context_text-Article-21925>, <Entity: 91 - context_text-Article-28274>, <Entity: 98 - context_text-Article-28499>, <Entity: 104 - context_text-Article-28598>, <Entity: 114 - context_text-Article-28610>, <Entity: 116 - context_text-Article-28649>, <Entity: 120 - context_text-Article-28741>, <Entity: 125 - context_text-Article-28846>]>))
- test_qs count: 112
- relation_qs count: 112

TODO

general TODO

general TODO:

  • methods to find relations, similar to filter_entities() and lookup_entities() in Entity model class. Include:

    • // Entity_Relation_Type by either slug or instance
    • // from =
    • from_in
    • from_type_in
    • from_identifiers_in
    • from_entity_traits

      • AND dictionary
      • OR dictionary
      • fancy
    • // to =

    • to_in
    • to_type_in
    • to_identifiers_in
    • to_entity_traits

      • AND dictionary
      • OR dictionary
      • fancy
    • // through =

    • through_in
    • through_identifiers_in
    • through_entity_traits

      • AND dictionary
      • OR dictionary
      • fancy
    • either FROM or TO (so undirected search - "I don't care which side")

      • IDs
      • identifiers
      • traits
    • any of FROM, TO, THROUGH

      • IDs
      • identifiers
      • traits
    • relation_traits

      • // AND dictionary
      • OR dictionary
      • fancy
    • NOTE for trait matching types (and probably entity identifiers, also):

      • "AND dictionary" - to start, accept a dictionary of trait names and values that all must match (AND match).
      • "OR dictionary" - could add an "OR" trait match dictionary as well.
      • "fancy" - Eventually, could add ability to spec trait name (or unsaved model instance?), then specify test for value (equals, contains, etc.) and test value. This would likely be a new little object.
    • Tags on Entity and/or Entity_Relation.

  • make small dummy person and organization classes in context, so I can use them to test Abstract_Entity_Container without needing context_text.

  • come up with better way to seed entities and relations for sourcenet - store spec in context_text base, then method to create or update all.

    • store the JSON from the context fixture in Context_Text_Base? Or in a sourcenet class somewhere?
    • make a class in context/shared that contains variables and methods to loops over the items in context fixture JSON and update the database based on what is inside. For each item in the fixture JSON, looks each up based on unique idnetifying information (name, slug, label, etc.). If it finds it, moves on. If not, creates it.
    • This will need to build up a basic object mapping based on foriegn keys before it creates anything, then create things in the right order (Entity_Types and related first, then Entity_Identifier_Types, then Relation_Types). Order:

      • context.Trait_Type \
      • context.Entity_Type \
      • context.Entity_Type_Trait \
      • context.Entity_Identifier_Type
      • context.Entity_Relation_Type \
      • context.Entity_Relation_Type_Trait \
      • context.Term_Relation_Type \
      • context.Vocabulary \
      • context.Term \
      • context.Term_Relation \
    • for each type:

      • make a map of id to fields for each item of that type.
      • make a method for creating an instance of that type from fields.
      • to associate related, retrieve instance from in-memory map based on ID, then if db_id present, use it to look up, else look up in database based on name. If not found, error, but could create.
    • To actually load, go in order of types outlined above, creating as you go.

    • When one of the items is added to the database, add a db_id field to their "fields".
  • abstraction:

    • make an abstract parent for a type that has associated trait specs (parent to Entity_Type and Entity_Relation_Type).

      • share method get_trait_spec().
    • make an abstract parent for trait containers that have associated types with associated trait specs (parent to Entity and Entity_Relation).

      • share method `

sourcenet-to-context TODO

Relation Creation TODO

  • FUTURE - abstract out the tests for an EntityContainer into their own parent test class that AbstractEntityContainers can use to re-use testing methods. Will need a way to abstract out traits, identifiers, etc. Not for now.
  • ? - make an "Abstract_Relation_Container" abstract method for code related to an instance of a given model resulting in a relation (Article_Data)?

    • Might need ManyToMany back links, will have to see how we do that.

DONE

general TODO DONE

general TODO DONE:

  • // take the "create_article_entity()" function and put it in a class for loading sourcenet articles into context

    • context_text/export/to_context_base/export_to_context.py - class ExportToContext
  • // build unit test class for this loading class, and add one for creating a fake article, then making an entity out of it. Check entity, traits, and identifiers.

  • // Add Entity ID foreign key to Article, Person, Newspaper, Organization models. Perhaps add it to a shared parent class? - context/shared/entity_models.py --> class Abstract_Entity_Container
  • add a unique_identifier_type column to Article model. To set for NewsBank:

      --SELECT COUNT( * ) FROM context_text_article;
      --SELECT COUNT( * ) FROM context_text_article WHERE archive_source = 'NewsBank';
      --SELECT * FROM context_text_article WHERE archive_source != 'NewsBank';
      --UPDATE context_text_article SET unique_identifier_type = 'article_newsbank_id' WHERE archive_source = 'NewsBank';
      --SELECT COUNT( * ) FROM context_text_article WHERE unique_identifier_type = 'permalink';
      --SELECT COUNT( * ) FROM context_text_article WHERE unique_identifier_type = 'article_newsbank_id';
    
    
    • need to update the code that loaded the NewsBank Articles so it sets this value.
    • also, need to make "Article" unit tests for entity creation, rather than just having it in the export unit tests?
  • 2019.11.07 - // method to find entity - based on type and identifier (accept all the fields that make sense, including optional identifier type instance).

    • // lookup_entities() - implemented, still need to write unit test.
    • // implement unit test test_lookup_entities() in test_entity_model, pattern off of the Entity_Identifier unit test test_filter_identifiers().
    • // update unit test test_get_entity_for_identifier() in test_entity_model to use all possible arguments.

sourcenet-to-context TODO DONE

Entity Creation TODO - DONE

Entity Creation TODO - DONE:

  • // Build and test basic article entity creation

    • // includes building out entity helper methods and testing context entity code.
    • // include adding reverse reference to Article's Entity to Article model.
    • // include creating Newspaper entity for related newspaper?
  • // Build and test basic newspaper entity creation

    • // ID type "newspaper_sourcenet_id" - add to both test and normal
    • // ID type "newspaper_newsbank_code" - add to both test and normal
    • // export new metadata fixture
    • // integrate into article creation.
  • // Build and test basic person entity creation

    • include adding reverse reference to Person's Entity to Person model.
  • // Build and test basic organization entity creation

    • include adding reverse reference to Organization's Entity to Organization model.
    • update newspaper entity creation to also create organization entity if newspaper.organization.has_entity() is False.

Relation Creation TODO - DONE

Relation Creation (start in export_to_context.py) TODO - DONE (All added 2019.11.25 unless noted otherwise):

  • // review the trait code and tests for entity (get, set, etc.), modify it and add it to relation, as well. It will work exactly the same (see if we can abstract somehow).
  • // make methods filter_relations and lookup_relations with subset of above to serve Entity_Relation creation.
  • create unit test class for Entity_Relation based on Entity that tests:

    • // trait methods
    • instance methods:

      • // set_basic_traits_from_dict()
    • class methods:

      • // Entity_Relation.create_entity_relation()

        • test 1 - make one with type of "quoted", set FROM, TO, THROUGH and traits.
        • test 2 - make one with same info, different traits, make sure it doesn't make a duplicate relation, and that trait values are updated.
        • try other permutations? No type, no through, no traits...
      • // filter_relations()

      • // lookup_relations()
  • Entity_Type and Entity_Relation_Type tests:

    • method get_type_for_slug()
  • // work through entity creation first in export_to_context.py:

    • // create_entities()
    • // create_newspaper_entities()
    • // create_article_entities()
    • // add coder ID, coder username, and coder type to all relations based on the Article_Data from which they are derived.
    • // create methods for getting lists of author, subject (including sources flag), and create test cases.
    • // create method to generate relaton trait dictionary from article and article_Data passed in.
    • // test cases for each of these in test_export_to_context.

      • // create_entity_container_entity()
      • // create_article_entity()
      • // create_person_entity()
      • // @classmethod make_author_entity_list()
      • // @classmethod make_relation_trait_dict()
      • // @classmethod make_subject_entity_list() (including only sources, and don't include sources in subjects).
      • // create_newspaper_relations()
      • // create_article_relations()
      • // create_relations()
      • // process_articles()
    • // hook these in to process_articles()

  • // add traits to some of the relations created in TestHelper.create_test_relations.

  • // need to test what happens when you call the update_entity() method on each entity a second time. Should not result in two separate entities.
  • 2019.12.04 - // add to unit test for process_articles() tests to make sure that tags are getting added to each article as they are processed.
  • 2019.12.04 - // create data from automated coding (just articles with coding by OpenCalais V.2) in actual database.