Choice dependent dynamic attributes model setup

I have come across many of the articles from this site on my (short) 6month django journey. They usually solved my questions so I am trying my luck here.

I have been grinding on an issue for weeks now. So much so I decided to create a workaround for the demo. The demo went well and I have a live project at work. And now, the problem returns to bite me in the a**.

Ill get right to the point and try and explain what I need:

  • I am trying to create a combined PIM / PricingModel webapp.
  • The pim mainly revolves around the model “ProductItem”

I need the ProductItem to have attribute fields like: (This is a fraction but enough to get the point)

  • inputs
  • outputs
  • uses_x_inputs
  • uses_x_outputs

That way I can run tests in the pricing-model-app which takes the equipment that uses_x_inputs and make sure you have the corresponding equipment that has enough inputs.

I have a model Type (ProductItem foreign key) which is the type of equipment (e.i. Camera, Sensor etc). I want to put the attributes as fields in this model and i would like them to be dynamic. This way if there is a new attribute I do not have to perform a migration.

On the admin/user side I would like to have:

  • The attributes to show up automatically when the user/admin chooses type.

A ProductItem would look like this:

ProductItem:
name = camera
description = seesthings
type = camera-analysis
    camera-analysis:
        <sensors|is_dynamic|has_default>=<1|is_dynamic|has_default>
        <Input|is_dynamic|has_default>=<2|is_dynamic|has_default>
        <UserNeedsNewField>

How would you go about building this Model/DB/admin structure.
Please feel free to ask ANY questions.

PS: I dont need the answer on a silver platter, I need help finding the right structure for further down the line so i dont have to micromanage models everytime someone has a product that has new subattributes, so to speak ;).

I will try to come back to your question when I have a little bit more time, but meanwhile I will share with you an implementation that I did maybe 6 years ago or something

I have this case, I think it should be more or less the same for you. Here the user can create a custom “model” with whatever field they want:

(See the add field / edit / remove, etc)

And then later on the user can use this form to collect data:

I achieved that with three models:

class DataExtractionField(models.Model):
    BOOLEAN_FIELD = 'B'
    STRING_FIELD = 'S'
    FLOAT_FIELD = 'F'
    INTEGER_FIELD = 'I'
    DATE_FIELD = 'D'
    SELECT_ONE_FIELD = 'O'
    SELECT_MANY_FIELD = 'M'
    FIELD_TYPES = (
        (BOOLEAN_FIELD, 'Boolean Field'),
        (STRING_FIELD, 'String Field'),
        (FLOAT_FIELD, 'Float Field'),
        (INTEGER_FIELD, 'Integer Field'),
        (DATE_FIELD, 'Date Field'),
        (SELECT_ONE_FIELD, 'Select One Field'),
        (SELECT_MANY_FIELD, 'Select Many Field'),
        )

    review = models.ForeignKey(Review)
    description = models.CharField(max_length=255)
    field_type = models.CharField(max_length=1, choices=FIELD_TYPES)
    order = models.IntegerField(default=0)

class DataExtractionLookup(models.Model):
    field = models.ForeignKey(DataExtractionField)
    value = models.CharField(max_length=1000)

class DataExtraction(models.Model):
    user = models.ForeignKey(User, null=True)
    article = models.ForeignKey(Article)
    field = models.ForeignKey(DataExtractionField)
    value = models.TextField(blank=True, null=True)
    select_values = models.ManyToManyField(DataExtractionLookup)

So DataExtractionField is the dynamic field definition, the DataExtractionLookup is to create a pre defined list of values for select one / select multiple options. And the model DataExtraction is to store the user inputted value.

Not sure if it’s the best solution, but works pretty well

You can see the code here:

The app is live here:

The code is not very well written, but you can get the idea from there

1 Like

Thank you for your quick reply, and with that renewed hope. This looks pretty much like what I need to accomplish, and ill dig deeper in to the code.

I can see much despair in my question and I will clean it and phrase it better tonight so its a bit more readable.

I can see that you use MustacheJs with this project but I will be every happy if you can take your time to do more tutorial on which Front-end Technology that is best and simple to use with Django. Even, if it is Jquery + Ajax we need more comprehensive Tutorial on that. Thanks