0

I am trying to build a Django website where the user is able to create custom objects known as items. Each item needs to be able to have certain properties that are stored in the database. For example an item would need properties such as

Serial Number, Description, Manufacture Date

However I want the user to be able to specify these fields similar to what Microsoft dynamics allows . For example a user should be able to specify they want a text field with the name Model Number, associated with a specific item type and from then on they can store those properties in the database.

I am not sure the best approach to do this because a standard database model, you already have all the fields defined for a specific table, however this essentially means i have to find a way to have user defined tables.

Does anyone know a good approach to handle this problem, at the end of the day I want to store items with custom properties as defined by the user in a database.

thanks

netzero
  • 73
  • 4

1 Answers1

0

There are multiple ways you can go.

In non-relational databases you don't need to define all the fields for a collections ( analogous to a table of RDBMS).

But if you want to use SQL with Django, then you can define a Property Model.

class Property(models.Model):
    name = CharField()
    value = CharField()
    item = models.ForeignKey(Item, on_delete=models.CASCADE)

class Item(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)

You can render a FormSet of Property form. To add extra empty forms on the fly, render dynamic formsets.

narendra-choudhary
  • 4,582
  • 4
  • 38
  • 58