1

Let's say I have a django-mptt model that looks like this:

class Category(MPTTModel):
    name = models.CharField(max_length=50)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

These Categories (and sub-categories) will serve as a kind of template for the categories used in a project. When a user starts a new project, the user will select which Categores, Sub-Categories, etc. to use. The user should be able to also add/edit Categories. The thing is that they need to be specific the project so that when another project is created, the user will start with the original/default categories.

Is there any way to duplicate the MPTTModel/database table(s) to create project specific one where categories can be edited/added without it affecting the default ones?

I can think of one way to solve this issue which would be to add something like

projects = models.ManyToManyField(Project)

and create a default/template project. What's the best approach here?

dlinx90
  • 855
  • 4
  • 14
  • 24

1 Answers1

0

You have the right idea having a "projects" field to match a category with various projects. You might be better off using a "project" field with a ForeignKey relationship:

project = models.ForeignKey(project)

Then you could duplicate the Category, creating a new database row, for each new project. That way if someone working on project foo changes the Category bar it doesn't affect project baz which could also be using Category bar. Both Category bar entries will have different primary keys in the database.

You could also have a project called "default", All the "default" Categories can get duplicated whenever a user creates a new project.

Of course you'll need code to display only Categories from the project your user is working on and to duplicate the Categories when a new project is created.

Jeremy S.
  • 1,086
  • 8
  • 18