Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use ModelSchema to create a dynamic model based from a exist Abstract Model? #72

Open
ramwin opened this issue Oct 27, 2023 · 1 comment

Comments

@ramwin
Copy link

ramwin commented Oct 27, 2023

Here is my scenario:

I want to create task table for every user, since each user may add this own dynamic fields.

def create_task_list(request):
    schema = ModelSchema.objects.create(name=f"task_{request.user.id}")
    # create common fields
    FieldSchema.objects.create(model_schema=schema, name="status", data_type="character")
    ...also create common fields like create_datetime, user, update_datetime...
    # create user fields
    for field in request.POST['fields']:
        ...

How can I create an abstract model like

class TaskModel:
    status = models.CharField()
    create_datetime = models.DateTimeField(auto_now_add=True)
    update_datetime = models.DateTimeField(auto_now=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

and use this model as a baseclass to create dynamic model like:

def create_task_list(request):
    schema = ModelSchema.objects.create(name=f"task_{request.user.id}", base_model=TaskModel)

    # create user fields
    for field in request.POST['fields']:
        ...
@ramwin
Copy link
Author

ramwin commented Oct 27, 2023

I reviewed the source code of dynamic-models, the factor will use the config.default_fields, but this will change behaviour for all dynamic models. But I only want the user's personal task to inherate TaskModel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant