how to use migrations in runtime for models creation?

44 views
Skip to first unread message

Prashanth Patelc

unread,
Feb 23, 2022, 5:39:54 AM2/23/22
to django...@googlegroups.com
hey ,
im working on dynamic models , im receiving model name and fields in front-end

eg:
class Users(models.Model):
     name=models.Charfield(max_length=30)
     last_name=models.Charfield(max_length=30)
     ,,,,,,,,,,,
after this i dont need to save into the models.py i need to store the data(create the table name with some fields into)postgreesql db
i need to migrate all the table(makemigrations,&migrate)

this is my code please check

from django.core.management import call_command

class CreateQueries(APIView):
def post(self, request, format=None):

table_name = request.data['table_name'] # im getting the table name here
print(table_name,'here printing')
# model_name = '%d_%s' % (connection.tenant.id, table_name)
class Meta:
db_table = table_name
managed = False
attrs = {
'u_id': models.CharField(max_length=200),
'u_name': models.CharField(max_length=200),
'u_email': models.CharField(max_length=200),
'u_password': models.CharField(max_length=200),
'__module__': 'dynamicapp.models',
'Meta': Meta
} this is my cutom fields ,further i will receive from front-end


model = type(str(table_name), (models.Model,), attrs)


call_command('makemigrations')
call_command('migrate')
Thanks in advance
Reply all
Reply to author
Forward
0 new messages