0

My requirement is: create category and product table according to the store selected.

class Category(models.Model):
  parent = models.ForeignKey("Category", blank=True, null=True, 
      related_name="children", verbose_name=_("Parent Category"))
  name = models.CharField(verbose_name=_("Name"), max_length=255)
  path = models.CharField(verbose_name=_("Path"), max_length=200)
  description = models.TextField(verbose_name=_("Description"))

class CategoryStore(model.Model):
  default = models.ForeignKey("Category", related_name="store_category", 
      verbose_name=_("Default Value"))
  parent = models.ForeignKey("CategoryStore", blank=True, null=True, 
      related_name="children", verbose_name=_("Parent Category"))
  name = models.CharField(verbose_name=_("Name"), max_length=255)
  description = models.TextField(verbose_name=_("Description"))
  store = models.ForeignKey("Store", related_name="store_category", 
      verbose_name=_("Store"))

CategoryStore model will have table names as category_store_1, category_store_2, etc. Would like to create the tables on the fly after a store is created. If, we add another field to the model, should have option to add to all the tables the new field.

class Product(models.Model):
  name = models.CharField(verbose_name=_("Name"), max_length=255)
  description = models.TextField(verbose_name=_("Description"))

class ProductStore(model.Model):
  default = models.ForeignKey("Product", related_name="store_product", 
      verbose_name=_("Default Value"))
  name = models.CharField(verbose_name=_("Name"), max_length=255)
  description = models.TextField(verbose_name=_("Description"))
  store = models.ForeignKey("Store", related_name="store_product", 
      verbose_name=_("Store"))

This also has same requirement as the category. One addition is that, option to create new field to both the model, new field on the fly from admin form. It will be attributes for the product. Instead of another table to store attribute values, want to add a new field to the Product and ProductStore model on the fly.

santhoshnsscoe
  • 207
  • 2
  • 7

1 Answers1

1

Not sure if I fully understand your problem, but it seems to me that you should reconsider your database structure. I'm not very experienced but adding tables to db on a regular basis on the fly is not good.

CategoryStore model will have table names as category_store_1, category_store_2, etc.

Keeping in mind that in django 1 model <=> 1 db table, try this approach: one table called CategoryStore which holds data from all your stores and another table called CategoryStoreType with the following structures:

CategoryStore has all the fields you need plus a type field which is a foreign key to CategoryStoreType. CategoryStoreType contains a short description of each store type, e.g. a tuple ('id', 'type name'). This way when you need a new field for one store type you simply add another field to CategoryStore, which holds data of all your store types. (Again, as for tables, adding fields on the fly is not that good, imho.)

Sirion
  • 804
  • 1
  • 11
  • 33
  • I agree with this answer. Django works best when models and tables are 1:1. Perhaps a non-relation database like MongoDB is a better solution for dynamic fields. – arocks May 03 '15 at 15:04
  • Thanks for the reply. Currently have used single table and static fields, later need to change as per the database size and requirement. Might go for MongoDb or django-mutant once they are ready for django 1.8, Checking http://stackoverflow.com/questions/7933596/django-dynamic-model-fields/7934577#7934577 and http://dynamic-models.readthedocs.org/en/latest/ – santhoshnsscoe May 08 '15 at 03:32