Comments (1)
Whole models:
from django.utils.translation import gettext_lazy as _
from django.db import models
from simple_history.models import HistoricalRecords
from {{ package.root_package.name | snakecase }}.validators import validate_even
{% for cls in package.classes %}{% if cls.generalization != None %}{% if cls.generalization.package != package %}
from {{ cls.generalization.package.name | case_package }}.models import {{ cls.generalization.name | case_class }}{% endif %}{% endif %}{% endfor %}
{% for enum in package.enumerations %}class ENUM_{{ enum.name | case_class }}(models.TextChoices):
{% for attr in enum.values %}
{{ attr.replace('-','').replace('/','') | snakecase | uppercase }} = '{{ attr }}', _('{{ attr }}'){% endfor %}
{% endfor %}
{% for cls in package.classes %}{% if cls.is_abstract %}class {{ cls.name | case_class }}( models.Model ):
{% for attr in cls.attributes %}
{% if attr.classification %} {{ attr.name | snakecase }} = models.CharField( max_length=100, choices=ENUM_{{ attr.classification.name | case_class }}.choices, blank=True, null=True )
{% else %} {{ attr.name | snakecase }} = models.{% if attr.stereotype == "auto" %}AutoField{% else %}{{ attr.dest_type }}{% endif %}( {% if attr.is_id %}primary_key=True, {% else %}blank=True, null=True, {% endif %}{% if attr.length %}max_length={{ attr.length }}{% endif %} )
{% endif %}{% endfor %}
{% if cls.is_abstract %} class Meta:
abstract = True
{% endif %}{% endif %}
{% endfor %}
{% for cls in package.classes %}{% if not cls.is_abstract %}class {{ cls.name | case_class }}( {% if cls.generalization %}{{ cls.generalization.name | case_class }}{% else %}models.Model{% endif %} ):
{% for attr in cls.attributes %}
{% if attr.classification %} {{ attr.name | snakecase }} = models.CharField( max_length=100, choices=ENUM_{{ attr.classification.name | case_class }}.choices, blank=True, null=True )
{% else %} {{ attr.name | snakecase }} = models.{% if attr.stereotype == "auto" %}AutoField{% else %}{{ attr.dest_type }}{% endif %}( {% if attr.is_id %}primary_key=True, {% else %}blank=True, null=True, {% endif %}{% if attr.dest_type == "DecimalField" %}max_digits=10, decimal_places=2, {% endif %}{% if attr.length %}max_length={{ attr.length }}{% endif %}{% if attr.validations != [] %}validators=[validate_even]{% endif %} )
{% endif %}{% endfor %}
{% for rel in cls.associations_from %}{% if rel.association_type.name == "ASSOCIATION" %}
{% if rel.cardinality.name == "ONE_TO_ONE" %}{{ rel.destination_name | snakecase }} = models.OneToOneField( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', on_delete=models.CASCADE, blank=True, null=True )
{% elif rel.cardinality.name == "MANY_TO_ONE" %}{{ rel.destination_name | snakecase }} = models.ForeignKey( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', related_name='{{ rel.source_name | snakecase }}', on_delete=models.CASCADE, blank=True, null=True )
{% elif rel.cardinality.name == "MANY_TO_MANY" %}{{ rel.destination_name | snakecase }} = models.ManyToManyField( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', blank=True ){% endif %}
{% elif rel.association_type.name == "COMPOSITION" %}
{% if rel.cardinality.name == "ONE_TO_ONE" %}{{ rel.destination_name | snakecase }} = models.OneToOneField( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', related_name='{{ rel.source_name | snakecase }}', on_delete=models.CASCADE )
{% else %}{{ rel.destination_name | snakecase }} = models.ForeignKey( '{{ rel.destination.package.name | case_package }}.{{ rel.destination.name | case_class }}', related_name='{{ rel.source_name | snakecase }}', on_delete=models.CASCADE ){% endif %}
{% endif %}{% endfor %}
{% for rel in cls.associations_to %}{% if rel.association_type.name != "COMPOSITION" %}
{% if rel.cardinality.name == "ONE_TO_MANY" %}{{ rel.source_name | snakecase }} = models.ForeignKey( '{{ rel.source.package.name | case_package }}.{{ rel.source.name | case_class }}', on_delete=models.CASCADE, related_name='{{ rel.destination_name | snakecase }}', blank=True, null=True ){% endif %}
{% endif %}{% endfor %}
{% if 'auditable' in cls.stereotypes %}
history = HistoricalRecords(){% endif%}
{% if cls.is_abstract %} class Meta:
abstract = True
{% endif %}{% endif %}
{% endfor %}
from pymdg.
Related Issues (20)
- camelCase returning PascalCase
- Ability to generate test case data in yaml
- UML 2.5 compliant ownership vs navigability
- Django model performant full text search based on "searchable" stereotype
- Avro template: Default value for union type must match first type from the union HOT 1
- Django models template: Inherited class should use case_class
- Add default string length to config and lower() the constraint.
- Add asyncapi template
- SparxDB root_package not being used
- Create Pydantic template HOT 2
- Avro list of objects at root not valid HOT 1
- Django template: DateTimeField from UML Timestamp
- Django models template: import enums from other packages if required
- Django model template: Unique together from attribute and relationship UniqueKey stereotype HOT 2
- Add association stereotypes
- python dialect bool
- Django dialect add Integer
- Add attribute flags to parser: is derived
- UMLClass phase causing dumps error as Decimal type not in default types HOT 1
- Key mapping
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pymdg.