Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom column names on Django ManyToMany fields

I am building a UI for a java application in django. I want to create Java tables according to the ones created by JPA. For this when i use the ManyToMany Field, the newly created intermediate table has column names which are not matching the ones with the JPA.

Is there any way that i can give custom names to the columns in the table So, as to not get the Missing column error in Java.

Any help would be really appreciated.

like image 637
Akshay Avatar asked Dec 01 '22 18:12

Akshay


1 Answers

You can specify a custom through model without specifying any extra fields. Then you can use db_column=... in the ForeignKey fields:

class A(models.Model):
    ...

class B(models.Model):
    a = models.ManyToManyField(A, through='C', db_table='customtablename')

class C(models.Model):
    a = models.ForeignKey(A, db_column='customcolumnname')
    b = models.ForeignKey(B, db_column='secondcustomcolumnname')
like image 88
knbk Avatar answered Dec 29 '22 01:12

knbk