Wednesday, February 23, 2022

how to use migrations in runtime for models creation?

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMCU6Cr_zUvnbhxKL-2k6mw7Rah9MLq%3DFZeGpBf-u1zt9EaBVg%40mail.gmail.com.

No comments:

Post a Comment