jsonschema2dj/relationships.py
def build_models(relationships: Dict[
    str,
    Tuple[
        Dict[str, Tuple[str, bool]],
        Dict[str, Tuple[str, bool]],
        List[Tuple[str, bool, Dict]],
    ],
]) -> Dict[str, List[Field]]:
    """converts the result of `extract_relationships` into a dictionary
    of objects where the keys are model names and the values are dict-like
    representation of django model relationships

    example argument:
    >>> {
    >>>     'Patient': (
    >>>         {'Doctor': ('doctor', True)},
    >>>         {'Address': ('address', True)},
    >>>         []
    >>>     ),
    >>>     'Doctor': ({}, {}, []),
    >>>     'Address': (
    >>>         {'Patient': ('patient', True)},
    >>>         {},
    >>>         []
    >>>     )
    >>> }

    example result
    >>> {
    >>>     'Patient': ["<doctor = models.ForeignKey("Doctor", null=True, on_delete=models.CASCADE)>"],
    >>>     'Doctor': [],
    >>>     'Address': ["<patient = models.ForeignKey("Patient", null=True, on_delete=models.CASCADE)>"]
    >>> }
    """

    models: Dict[str, List[Field]] = {
        model: [] for model in relationships
    }  # we are going to return this

    # we loop through all models, extracting singlular and multiple relationships
    for model, (singular, multiples, json) in relationships.items():
        # for all singular relations...
        for single, (single_name, null) in singular.items():
            # we fetch the other side of the relationship
            related_single = relationships[single][0]
            # the other side is also singular?
            if model in related_single:
                # ten this is one-to-one
                models[model].append(
                    Relationship("OneToOneField", single_name, single, null)
                )
            # no? then its one-to-many
            else:
                models[model].append(
                    Relationship("ForeignKey", single_name, single, null)
                )

        # as above but....
        for many, (many_name, null) in multiples.items():
            related_single = relationships[many][0]
            if model not in related_single:
                models[model].append(
                    Relationship("ManyToManyField", many_name, many, null)
                )
            # ... no opposite case, if its already as a single field then this
            # many-to-one relationship wil get picked up anyway

        for name, null, schema in json:
            models[model].append(JSONField(name, null=null, schema=schema))

    return models
converts the result of `extract_relationships` into a dictionary
    of objects where the keys are model names and the values are dict-like
    representation of django model relationships

    example argument:
    >>> {
    >>>     'Patient': (
    >>>         {'Doctor': ('doctor', True)},
    >>>         {'Address': ('address', True)},
    >>>         []
    >>>     ),
    >>>     'Doctor': ({}, {}, []),
    >>>     'Address': (
    >>>         {'Patient': ('patient', True)},
    >>>         {},
    >>>         []
    >>>     )
    >>> }

    example result
    >>> {
    >>>     'Patient': ["<doctor = models.ForeignKey("Doctor", null=True, on_delete=models.CASCADE)>"],
    >>>     'Doctor': [],
    >>>     'Address': ["<patient = models.ForeignKey("Patient", null=True, on_delete=models.CASCADE)>"]
    >>> }
    
converts the result of `extract_relationships` into a dictionary
of objects where the keys are model names and the values are dict-like
representation of django model relationships




converts the result of ` extract_relationships ` into a dictionary of objects where the keys are model names and the values are dict - like representation of django model relationships
def build_models(relationships: Dict[
    str,
    Tuple[
        Dict[str, Tuple[str, bool]],
        Dict[str, Tuple[str, bool]],
        List[Tuple[str, bool, Dict]],
    ],
]) -> Dict[str, List[Field]]:
    models: Dict[str, List[Field]] = {
        model: [] for model in relationships
    }  
    for model, (singular, multiples, json) in relationships.items():
        for single, (single_name, null) in singular.items():
            related_single = relationships[single][0]
            if model in related_single:
                models[model].append(
                    Relationship("OneToOneField", single_name, single, null)
                )
            else:
                models[model].append(
                    Relationship("ForeignKey", single_name, single, null)
                )
        for many, (many_name, null) in multiples.items():
            related_single = relationships[many][0]
            if model not in related_single:
                models[model].append(
                    Relationship("ManyToManyField", many_name, many, null)
                )
        for name, null, schema in json:
            models[model].append(JSONField(name, null=null, schema=schema))
    return models
def build_models ( relationships : Dict [ str , Tuple [ Dict [ str , Tuple [ str , bool ] ] , Dict [ str , Tuple [ str , bool ] ] , List [ Tuple [ str , bool , Dict ] ] , ] , ] ) -> Dict [ str , List [ Field ] ] : models : Dict [ str , List [ Field ] ] = { model : [ ] for model in relationships } for model , ( singular , multiples , json ) in relationships . items ( ) : for single , ( single_name , null ) in singular . items ( ) : related_single = relationships [ single ] [ 0 ] if model in related_single : models [ model ] . append ( Relationship ( "OneToOneField" , single_name , single , null ) ) else : models [ model ] . append ( Relationship ( "ForeignKey" , single_name , single , null ) ) for many , ( many_name , null ) in multiples . items ( ) : related_single = relationships [ many ] [ 0 ] if model not in related_single : models [ model ] . append ( Relationship ( "ManyToManyField" , many_name , many , null ) ) for name , null , schema in json : models [ model ] . append ( JSONField ( name , null = null , schema = schema ) ) return models
converts the result of `extract_relationships` into a dictionary
of objects where the keys are model names and the values are dict-like
representation of django model relationships
converts the result of ` extract_relationships ` into a dictionary of objects where the keys are model names and the values are dict - like representation of django model relationships
"""converts the result of `extract_relationships` into a dictionary
    of objects where the keys are model names and the values are dict-like
    representation of django model relationships

    example argument:
    >>> {
    >>>     'Patient': (
    >>>         {'Doctor': ('doctor', True)},
    >>>         {'Address': ('address', True)},
    >>>         []
    >>>     ),
    >>>     'Doctor': ({}, {}, []),
    >>>     'Address': (
    >>>         {'Patient': ('patient', True)},
    >>>         {},
    >>>         []
    >>>     )
    >>> }

    example result
    >>> {
    >>>     'Patient': ["<doctor = models.ForeignKey("Doctor", null=True, on_delete=models.CASCADE)>"],
    >>>     'Doctor': [],
    >>>     'Address': ["<patient = models.ForeignKey("Patient", null=True, on_delete=models.CASCADE)>"]
    >>> }
    """ # we are going to return this # we loop through all models, extracting singlular and multiple relationships # for all singular relations... # we fetch the other side of the relationship # the other side is also singular? # ten this is one-to-one # no? then its one-to-many # as above but.... # ... no opposite case, if its already as a single field then this # many-to-one relationship wil get picked up anyway
relationships
Dict[
    str,
    Tuple[
        Dict[str, Tuple[str, bool]],
        Dict[str, Tuple[str, bool]],
        List[Tuple[str, bool, Dict]],
    ],
]