How to adapt java POJO generator for Django model with custom methods?

288 Views Asked by At

I am done with this tutorial which shows how to use textX to write a java POJO.

I am thinking of adapting this to write Django model classes.

The issue is when I have to add custom model methods to the Django model class.

And then if I subsequently change the DSL, the generated model class will definitely remove the custom model methods.

How to I cater for custom model methods if using textX to generate Django model classes?

1

There are 1 best solutions below

1
On

Well, you could use a diff/merge tool to merge the existing and newly-generated code but that will certainly require manual interventions to solve eventual merge conflicts.

FWIW, writing your model classes manually won't take much more time than writing them using a DSL (I slightly changed the DSL version to account for database-specific needs like charfields length):

entity Person {
  name : string(128)       
  address: Address   
  age: integer      
}

vs from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=128)
    address = models.ForeignKey(Address)
    age = models.IntegerField()

so I really don't think you'll gain anything with code generation here.

NB: if your problem is to generate "boostrap" models code from an existing DB schema, Django already knows how to do this without the need for a DSL.