{"id":16603822,"url":"https://github.com/tuffnatty/xsd_to_django_model","last_synced_at":"2025-03-21T13:32:53.127Z","repository":{"id":62590177,"uuid":"58455936","full_name":"tuffnatty/xsd_to_django_model","owner":"tuffnatty","description":"Generate Django models from an XSD schema description (and a bunch of hints)","archived":false,"fork":false,"pushed_at":"2024-01-20T04:01:08.000Z","size":301,"stargazers_count":25,"open_issues_count":4,"forks_count":11,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-18T01:51:48.022Z","etag":null,"topics":["django-orm","xsd"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tuffnatty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-10T11:33:37.000Z","updated_at":"2024-10-15T11:29:41.000Z","dependencies_parsed_at":"2024-10-28T10:29:18.076Z","dependency_job_id":"e88873e1-1d28-40d5-b92e-6cec7662c36a","html_url":"https://github.com/tuffnatty/xsd_to_django_model","commit_stats":{"total_commits":134,"total_committers":3,"mean_commits":"44.666666666666664","dds":0.02985074626865669,"last_synced_commit":"f70e2f77a15e78b24c23a38627599e970d788742"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuffnatty%2Fxsd_to_django_model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuffnatty%2Fxsd_to_django_model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuffnatty%2Fxsd_to_django_model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tuffnatty%2Fxsd_to_django_model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tuffnatty","download_url":"https://codeload.github.com/tuffnatty/xsd_to_django_model/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244806192,"owners_count":20513398,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django-orm","xsd"],"created_at":"2024-10-12T00:53:14.079Z","updated_at":"2025-03-21T13:32:52.788Z","avatar_url":"https://github.com/tuffnatty.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# xsd_to_django_model\nGenerate Django models from an XSD schema description (and a bunch of hints)\n\n## TODO\n* More examples.\n* More heuristics.\n* Better `xs:complexType/(xs:simpleContent|xs:complexContent)/xs:restriction` support.\n* `xs:simpleType/xs:union` support.\n* ...?\n\n## Getting started\n\n```\nUsage:\n    xsd_to_django_model.py [-m \u003cmodels_filename\u003e] [-f \u003cfields_filename\u003e] [-j \u003cmapping_filename\u003e] \u003cxsd_filename\u003e \u003cxsd_type\u003e...\n    xsd_to_django_model.py -h | --help\n\nOptions:\n    -h --help              Show this screen.\n    -m \u003cmodels_filename\u003e   Output models filename [default: models.py].\n    -f \u003cfields_filename\u003e   Output fields filename to build custom Django field classes.\n    -j \u003cmapping_filename\u003e  Output JSON mapping filename [default: mapping.json].\n    \u003cxsd_filename\u003e         Input XSD schema filename.\n    \u003cxsd_type\u003e             XSD type (or, if starting with `/`, the name of the toplevel element of the type) for which a Django model should be generated.\n\nIf you have xsd_to_django_model_settings.py in your PYTHONPATH or in the current directory, it will be imported.\n```\n\n## Examples\n\nSee the `examples` subdirectory.\n\n## Settings\n\nIf you have `xsd_to_django_model_settings.py` in your `PYTHONPATH` or in the current directory, it will be imported.\nIt may define the following module-level variables:\n\n* `MAX_LINE_LENGTH` is maximum line length in generated Python code. Wrapping works for most of the cases; probably one should use black if that matters anyways.\n* `TYPE_MOODEL_MAP` is a `collections.OrderedDict` (you can use `dict` but then processing order is not guaranteed) which maps XSD `xs:complexType` names (including ones autogenerated for anonymous `xs:complexType`s) to Django model class names, using regular expressions, e.g., if the XSD types are all called like `tMyStrangeTasteType`, this will generate model names like `MyStrangeTaste`:\n  ```python\n  from collections import OrderedDict\n  \n  TYPE_MODEL_MAP = OrderedDict([\n      (r't([A-Z].*)Type', r'\\1'),\n  ])\n  ```\n  If you are mapping different XSD types to a single Django model, prefix the Django model's name with `+` to make sure model merging logic works well.\n\n* `MODEL_OPTIONS` is a `dict` mapping model names to `dict`s of options applied when processing this model:\n  ```python\n  MODEL_OPTIONS = {\n      'MyStrangeTaste': {\n          'abstract': True,\n      },\n  }\n  ```\n  Supported model options:\n  * `abstract` - when `True`, enforce `abstract = True` in Django model`s `Meta`.\n  * `add_fields` - a `list` of extra fields which should be included in the final Django model class. Most of the options should be set in these definitions, overrides won't work. Example:\n    ```python\n    'add_fields': [\n        {\n            'name': 'content_md5',                # Django model field name\n            'dotted_name': 'a.any',               # path to the field in XML\n            'django_field': 'models.UUIDField',   # Django field class\n            'doc': 'A hash to speed up lookups',  # Django field's verbose_name\n            'options': ['db_index=True'],         # Django field's options\n        },\n    ],\n    ```\n  * `add_json_attrs` - a mapping of extra JSON attributes to their documentation.\n  * `array_fields` - a `list` of XSD fields for which `django.contrip.postgres.fields.ArrayField`s should be generated. The corresponding `xs:element` should either have `maxOccurs=\"unbounded\"` or contain an `xs:complexType` whose only member has `maxOccurs=\"unbounded\"`.\n  * `coalesce_fields` - a `dict` mapping generated field names to actual field names in Django models, using regular expressions, e.g.:\n    ```python\n    'coalesce_fields': {\n        r'a_very_long_autogenerated_prefix_(.+)': r'myprefix_\\1',\n    },\n    ```\n  * `custom` - when `True`, treat the model as a custom model which does not have a corresponding `xs:complexType` in XSD schema. Makes sense to use at least `add_fields` as well in such case.\n  * `drop_fields` - a `list` of field names to be ignored when encountered, e.g.:\n    ```python\n    'drop_fields': ['digitalSignature2KBInSize'],\n    ```\n  * `field_docs` - a `dict` which allows to override `verbose_name` Django model field option for given fields:\n    ```python\n    'field_docs': {\n        'objectName': 'Fully qualified object name',\n    }\n    ```\n  * `field_options` - a `dict` mapping final Django model field names to a list of their overridden options, e.g.:\n    ```python\n    'field_options': {\n        'schemeVersion': ['max_length=7'],\n    }\n    ```\n  * `field_type_options` - a `dict` mapping a final Django model field type to a list of that type's overriden options, e.g.:\n    ```python\n    'field_type_options': {\n        'models.DecimalField': ['max_digits=20'],\n    }\n    ```  \n  * `flatten_fields` - a `list` of fields where the contained `xs:complexType` should not be treated as a separate Django model, but is to be merged in the current model, with member field names prefixed with this field's name and a `_`:\n    ```python\n    'flatten_fields': ['section1', 'section2'],\n    ```\n  * `flatten_prefixes` - a `list` of field name prefixes which trigger the same flattening logic as `flatten_fields`. I do not recommend to use this option as things get too much automatic with it.\n  * `foreign_key_overrides` - a `dict` mapping field names to XSD type names when this field is to be treated as a `ForeignKey` reference to that type`s corresponding model.\n  * `gin_index_fields` - a `list` of Django model fields for which indexes are defined in `Meta` using Django 1.11+ `indexes` and `django.contrib.postgres.indexes.GinIndex`.\n  * `if_type` - a `dict` mapping XSD type names to `dict`s of options which are used only when source XSD type name matches that type name; used by model merging logic to ensure merged fields do not differ in models being merged:\n    ```python\n    'if_type': {\n        'tMyStrangeTasteType': {\n            'abstract': True,\n        },\n    },\n    ```\n  * `ignore_merge_mismatch_fields`: a `list` of fields for which generated code differences are to be ignored when model merging takes place. I do not recommend to use this option as it gets harder to track changes over time.\n  * `include_parent_fields`: when `True`, the parent (`xs:complexType/xs:complexContent/xs:extension[@base]` type's fields are simply included in the current model instead of making Django model inheritance structures.\n  * `index_fields` - a `list` of Django model fields for which `db_index=True` option should be generated.\n  * `json_fields` - a `list` of XSD fields which do not get their own Django model fields but are all mapped to a single `attrs = django.db.models.JSONField()`.\n  * `many_to_many_field_overrides` - a `dict` mapping field names to XSD type names when this field is to be treated as a `ManyToManyField` reference to that type`s corresponding model.\n  * `many_to_many_fields` - a `list` of field names that get mapped to Django `ManyToManyField`s.\n  * `meta` - a `list` of Django model `Meta` options' string templates added to the generated model's `Meta`:\n    ```python\n    'meta': [\n        'db_table = \"my_%(model_lower)s\"',\n    ]\n    ```\n    Template variables:\n    * `model_lower` resolves to lowercased model name.\n  * `methods` - a list of strings that are included as methods in the generated model class, e.g.:\n    ```python\n    'methods': [\n        '    def __unicode__(self): return \"%s: %s\" % (self.code, self.amount)',\n    ],\n    ```\n  * `null_fields` - a `list` of field names for which `null=True` Django model field option should be enforced.\n  * `one_to_many_field_overrides` - a `dict` mapping one-to-many relationship field names to related Django model names when automated logic does not work well.\n  * `one_to_many_fields` - a `list` of field names that get mapped to one-to-many relationships in Django, that is, a `ForeignKey` to this model is added to the related model's fields.\n  * `one_to_one_fields` - a `list` of field names that get mapped to one-to-one (shared primary key) relationships in Django, that is, a `OneToOneField(primary_key=True)` to this model is added to the related models's fields.\n  * `override_field_class` - a `dict` mapping field names to corresponding Django field class names when automated logic does not work well, e.g.:\n    ```python\n    'override_field_class': {\n        'formattedNumber': 'models.TextField',  # The actual data does not meet maximum length restriction\n    }\n    ```\n  * `parent_type` - an XSD `xs:complexType` name which is to be used as this model's parent type when automated logic does not work, e.g., when `custom` is `True`.\n  * `plain_index_fields` - a `list` of Django model fields for which indexes are defined in `Meta` using Django 1.11+ `indexes` and `models.Index`. This is useful when you don't need extra index for `LIKE` queries autogenerated with `db_index=True` option.\n  * `primary_key` specifies a single field name for which `primary_key=True` Django model field option is to be generated.\n  * `reference_extension_fields` - a `list` of field names which use `xs:complexType/xs:complexContent/xs:extension' inheritance schema but extend the base type with extra members; these fields will be mapped to a `ForeignKey` to their base type's model, and the extension members will be flattened (as in `flatten_fields`).\n  * `reverse_fields` - Sort fields in reflected lexicographic order to simplify merging fields from different flattened branches at mapping development time.\n  * `skip_code` - if `True`, the generated model code is to be skipped when saving the models module.\n  * `strategy` - if `1`, the strategy is to flatten all nested structures by default unless they refer to a toplevel model, or to a N:many relationship.\n  * `strict_index_fields` - a `list` of Django model fields to generate a composite index including the primary key.\n  * `unique_fields` - a `list` of Django model fields for which `unique=True` option should be generated.\n\n* `GLOBAL_MODEL_OPTIONS` is a `dict` of model options applied to each model (but may be overridden by the respective options in that very model):\n  ```python\n  GLOBAL_MODEL_OPTIONS = {\n      'json_fields': ['attachments', 'abracatabra'],\n  }\n  ```\n  Additional global options:\n  * `charfield_max_length_factor` (defaults to `1`) - a factor to multiply every `CharField`s `max_length` by, e.g. when actual XML data is bigger than XSD dictates.\n\n* `TYPE_OVERRIDES` is a `dict` mapping XSD type names to Django model fields when automatic heuristics don't work, e.g.:\n  ```python\n  TYPE_OVERRIDES = {\n      'xs:IDREF': ('A reference to xs:ID', 'CharField', {'max_length': 64}),\n  }\n  ```\n\n* `BASETYPE_OVERRIDES` is a `dict` mapping XSD base type names to Django field to override default ones, e.g.:\n  ```python\n  BASETYPE_OVERRIDES = {\n      'xs:hexBinary': 'CharField',\n  }\n  ```\n\n* `IMPORTS` is a string inserted after the automatically generated `import`s in the output models file, e.g.:\n   ```python\n   IMPORTS = \"from mycooldjangofields import StarTopologyField\"\n   ```\n\n* `DOC_PREPROCESSOR` is a function to preprocess documentation strings, e.g.:\n   ```python\n   def doc_preprocessor(s):\n       return s.replace('\\n', ' ')\n\n   DOC_PREPROCESSOR = doc_preprocessor\n   ```\n\n* `JSON_DOC_HEADING` is documentation heading template for `JsonField`s.\n\n* `JSON_GROUP_HEADING` is documentation attribute group heading template for `JsonField`s.\n\n* `JSON_DOC_INDENT` is indent prefix in documentation sublists. The default is 4 spaces, which is compatible between Markdown and reStructuredText.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuffnatty%2Fxsd_to_django_model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftuffnatty%2Fxsd_to_django_model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftuffnatty%2Fxsd_to_django_model/lists"}