Skip to content

Commit

Permalink
Added some usage/tweaking examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tuffnatty committed Aug 16, 2017
1 parent cbdc2de commit 3107839
Show file tree
Hide file tree
Showing 38 changed files with 844 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Generate Django models from an XSD schema description (and a bunch of hints)

## TODO
* More documentation.
* Examples.
* More examples.
* `<xs:complexType><xs:complexContent><xs:restriction>...` support.
* ...?

## Getting started

Expand All @@ -22,3 +24,7 @@ Options:
If you have xsd_to_django_model_settings.py in your PYTHONPATH or in the current directory, it will be imported.
```

## Examples

See the `examples` subdirectory.
10 changes: 10 additions & 0 deletions examples/defxmlschema/chapter04/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This is a sample XSD from http://www.datypic.com/books/defxmlschema/chapter04.html.

The resulting models look fairly well. Additional XSD files are automatically imported using `xs:import[@schemaLocation]` and `xs:include[@schemaLocation]`.

The settings file demonstrates:
* XSD typename to Django model name mapping (`TYPE_MODEL_MAP`).
* `flatten_fields` usage for `xs:complexType/xs:simpleContent/xs:extension` fields.
* `flatten_fields` usage for `xs:complexType/xs:complexContent` fields.

PYTHONPATH=. ../../../xsd_to_django_model/xsd_to_django_model.py chapter04ord1.xsd OrderType
11 changes: 11 additions & 0 deletions examples/defxmlschema/chapter04/chapter04cust.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="CustomerType">
<xs:sequence>
<xs:element name="name" type="CustNameType"/>
<xs:element name="number" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="CustNameType">
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:schema>
17 changes: 17 additions & 0 deletions examples/defxmlschema/chapter04/chapter04ord1.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/ord"
xmlns="http://example.org/ord"
xmlns:prod="http://example.org/prod">
<xs:include schemaLocation="chapter04ord2.xsd"/>
<xs:include schemaLocation="chapter04cust.xsd"/>
<xs:import namespace="http://example.org/prod"
schemaLocation="chapter04prod.xsd"/>
<xs:element name="order" type="OrderType"/>
<xs:complexType name="OrderType">
<xs:sequence>
<xs:element name="number" type="OrderNumType"/>
<xs:element name="customer" type="CustomerType"/>
<xs:element name="items" type="prod:ItemsType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
7 changes: 7 additions & 0 deletions examples/defxmlschema/chapter04/chapter04ord2.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.org/ord"
targetNamespace="http://example.org/ord">
<xs:simpleType name="OrderNumType">
<xs:restriction base="xs:string"/>
</xs:simpleType>
</xs:schema>
27 changes: 27 additions & 0 deletions examples/defxmlschema/chapter04/chapter04prod.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.org/prod"
targetNamespace="http://example.org/prod">
<xs:complexType name="ItemsType">
<xs:sequence>
<xs:element name="product" type="ProductType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="number" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="size" type="SizeType"/>
<xs:element name="color" type="ColorType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="SizeType">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="system" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="ColorType">
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
</xs:schema>
18 changes: 18 additions & 0 deletions examples/defxmlschema/chapter04/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# THIS FILE IS GENERATED AUTOMATICALLY. DO NOT EDIT
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import datetime
from django.core import validators
from django.db import models


class CustNameTypeField(models.TextField):
# Simple exact redefinition of TextField parent!
pass

class OrderNumTypeField(models.TextField):
# Simple exact redefinition of TextField parent!
pass

54 changes: 54 additions & 0 deletions examples/defxmlschema/chapter04/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# THIS FILE IS GENERATED AUTOMATICALLY. DO NOT EDIT
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import datetime
from django.core import validators
from django.db import models

from .fields import \
CustNameTypeField, \
OrderNumTypeField



# Corresponds to XSD type[s]: CustomerType
class Customer(models.Model):
name = CustNameTypeField("name")
number = models.IntegerField("number")


# Corresponds to XSD type[s]: prod:ProductType
class Product(models.Model):
number = models.IntegerField("number")
name = models.TextField("name")
size_system = models.TextField("size::size_system", null=True)
size = models.IntegerField("size")
color_value = models.TextField("color::color_value", null=True)


# Corresponds to XSD type[s]: prod:ItemsType
class Items(models.Model):
product = models.ForeignKey(
Product,
on_delete=models.PROTECT,
verbose_name="product"
)


# Corresponds to XSD type[s]: OrderType
class Order(models.Model):
number = OrderNumTypeField("number")
customer = models.ForeignKey(
Customer,
on_delete=models.PROTECT,
verbose_name="customer"
)
items = models.ForeignKey(
Items,
on_delete=models.PROTECT,
verbose_name="items"
)


10 changes: 10 additions & 0 deletions examples/defxmlschema/chapter04/xsd_to_django_model_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
TYPE_MODEL_MAP = {
r'prod:(.+)Type': r'\1',
r'(\w+)Type': r'\1',
}

MODEL_OPTIONS = {
'Product': {
'flatten_fields': ['color', 'size']
},
}
13 changes: 13 additions & 0 deletions examples/defxmlschema/chapter05/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This is a sample XSD from http://www.datypic.com/books/defxmlschema/chapter05.html.

The settings file demonstrates:
* XSD typename to Django model name mapping (`TYPE_MODEL_MAP`).
* `many_to_many_fields` usage.
* `primary_key` usage.
* `flatten_fields` usage for `xs:complexType/xs:simpleContent/xs:extension` fields.

An extra technique is demonstrated for different namespaces in multiple XSD files: a manually crafted "root" XSD file, `chapter05.xsd`, with `schemaLocation` references.

The resulting models look fairly well.

PYTHONPATH=. ../../../xsd_to_django_model/xsd_to_django_model.py chapter05.xsd OrderType
6 changes: 6 additions & 0 deletions examples/defxmlschema/chapter05/chapter05.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- manual hack to merge multiple xsd files - Ph. -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prod="http://example.org/prod">
<xs:import namespace="http://example.org/ord" schemaLocation="chapter05ord.xsd"/>
<xs:import namespace="http://example.org/prod" schemaLocation="chapter05prod.xsd"/>
</xs:schema>
17 changes: 17 additions & 0 deletions examples/defxmlschema/chapter05/chapter05ord.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/ord"
xmlns="http://example.org/ord"
xmlns:prod="http://example.org/prod">
<xs:import namespace="http://example.org/prod"/>
<xs:element name="order" type="OrderType"/>
<xs:complexType name="OrderType">
<xs:sequence>
<xs:element name="items" type="ItemsType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ItemsType">
<xs:sequence>
<xs:element ref="prod:product" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
19 changes: 19 additions & 0 deletions examples/defxmlschema/chapter05/chapter05prod.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.org/prod"
targetNamespace="http://example.org/prod">
<xs:element name="product" type="ProductType"/>
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="number" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="size" nillable="true" type="SizeType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="SizeType">
<xs:simpleContent>
<xs:extension base="xs:integer">
<xs:attribute name="system" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
10 changes: 10 additions & 0 deletions examples/defxmlschema/chapter05/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# THIS FILE IS GENERATED AUTOMATICALLY. DO NOT EDIT
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import datetime
from django.core import validators
from django.db import models


25 changes: 25 additions & 0 deletions examples/defxmlschema/chapter05/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# THIS FILE IS GENERATED AUTOMATICALLY. DO NOT EDIT
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import datetime
from django.core import validators
from django.db import models




# Corresponds to XSD type[s]: prod:ProductType
class Product(models.Model):
number = models.IntegerField("number", primary_key=True)
name = models.TextField("name")
size_system = models.TextField("size::size_system", null=True)
size = models.IntegerField("size")


# Corresponds to XSD type[s]: OrderType
class Order(models.Model):
items = models.ManyToManyField(Product, verbose_name="items")


14 changes: 14 additions & 0 deletions examples/defxmlschema/chapter05/xsd_to_django_model_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
TYPE_MODEL_MAP = {
r'prod:(.+)Type': r'\1',
r'(\w+)Type': r'\1',
}

MODEL_OPTIONS = {
'Order': {
'many_to_many_fields': ['items'],
},
'Product': {
'flatten_fields': ['size'],
'primary_key': 'number',
}
}
5 changes: 5 additions & 0 deletions examples/defxmlschema/chapter06/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is a sample XSD from http://www.datypic.com/books/defxmlschema/chapter06.html.

The resulting model looks fairly well.

PYTHONPATH=. ../../../xsd_to_django_model/xsd_to_django_model.py chapter06.xsd ProductType
19 changes: 19 additions & 0 deletions examples/defxmlschema/chapter06/chapter06.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.org/prod"
targetNamespace="http://example.org/prod"
elementFormDefault="qualified">
<xs:element name="product" type="ProductType"/>
<xs:element name="size" type="xs:integer" fixed="10"/>
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="name" default="N/A">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element ref="size" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
10 changes: 10 additions & 0 deletions examples/defxmlschema/chapter06/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# THIS FILE IS GENERATED AUTOMATICALLY. DO NOT EDIT
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import datetime
from django.core import validators
from django.db import models


18 changes: 18 additions & 0 deletions examples/defxmlschema/chapter06/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# THIS FILE IS GENERATED AUTOMATICALLY. DO NOT EDIT
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import datetime
from django.core import validators
from django.db import models




# Corresponds to XSD type[s]: ProductType
class ProductType(models.Model):
name = models.TextField("name", blank=False, default='N/A')
size = models.IntegerField("size", default=10, null=True)


5 changes: 5 additions & 0 deletions examples/defxmlschema/chapter07/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is a sample XSD from http://www.datypic.com/books/defxmlschema/chapter07.html.

The resulting model looks fairly well.

PYTHONPATH=. ../../../xsd_to_django_model/xsd_to_django_model.py chapter07.xsd SizeType
19 changes: 19 additions & 0 deletions examples/defxmlschema/chapter07/chapter07.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.org/prod"
targetNamespace="http://example.org/prod"
elementFormDefault="qualified">
<xs:element name="size" type="SizeType"/>
<xs:complexType name="SizeType">
<xs:attribute name="system" type="xs:string" use="required"/>
<xs:attribute ref="dim"/>
<xs:attribute name="value" default="10">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="2"/>
<xs:maxInclusive value="20"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:attribute name="dim" type="xs:integer" fixed="1"/>
</xs:schema>
10 changes: 10 additions & 0 deletions examples/defxmlschema/chapter07/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# THIS FILE IS GENERATED AUTOMATICALLY. DO NOT EDIT
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import datetime
from django.core import validators
from django.db import models


Loading

0 comments on commit 3107839

Please sign in to comment.