Thursday, June 29, 2017

Using the Django ORM, How can you create a unique hash for all possible combinations

Leave a Comment

I want to maintain a Django model with a unique id for every combination of choices within the model. I would then like to be able to update the model with a new field and not have the previous unique id's change. The id's can be a hash or integer or anything. What's the best way to achieve this?

class MyModel(models.Model):     WINDOW_MIN = 5     WINDOW_MAX = 7     WINDOW_CHOICES = [(i,i) for i in range(WINDOW_MIN - 1, WINDOW_MAX - 1)]     window = models.PositiveIntegerField('Window', editable=True, default=WINDOW_MIN, choices=WINDOW_CHOICES)     is_live = models.BooleanField('Live', editable=True, default=False)     unique_id = .... 

Given the above example there will be 3 * 2 == 6 unique id's. If I add another editable boolean field, I don't want to change the previous unique id's but I want the new unique id's to be generated for the new boolean field.

The thought process behind this is the parameters in MyModel define the inputs to a function who's results are stored in another Django model MyResultModel by unique_id and the name of the model. The reasoning behind this is there are multiple variants of MyModel each with it's own set unique combination's that get updated regularly but the result set in MyResultModel is the same across MyModel1 to MyModelN. Ideally I would like the unique_id's to be autogenerated. In other words the key for the result set stored in MyResultModel is the model_name (MyModel) and a unique_id. I want to sanely manage this many (MyModel1,...MyModelN) to one (MyResultModel) relationship.

class MyResultModel(models.Model):     unique_id = ...     model_name = models.CharField(max_length=200, default='', blank=False) # Points to a Django Model ex MyModel     result1 = ...     result2 = ... 

2 Answers

Answers 1

A common approach, given that all your options are boolean, categorical or small numbers, you could just pack them into a bigger bit field (https://en.wikipedia.org/wiki/Bit_field) and whenever you add a new option, make sure to push it to the most-significant part of your bit field and avoid having a value of 0 (that is, simple add 1 to whatever). Not only would every single unique_id represent a different configuration, you could even do without the model and just use bit operations to extract all your values from the bit field.

Answers 2

I have 2 thing: First, if you just want to create unique hash for all the combinations

# Just set it all together like uniq= cb1+cb2+cb3... (a string key is ok =) ) # If it is very very long string ~> we think about using hash to short it (and have same size for all value). 

And next:

For your question above:

i can't understand why do you make the model into a complicate thing (like a mess) but still have to solve it:

As i read your ques: you have many variants of Models ~> want to gather it into 1 model res?

So: it is better to set FK

map = models.ForeignKey(Model_name, null=True) # but if you have too many model, it can't be help... 

So i recomment:

~ create just 1 model, because if you have too many model, you can't even call it to get data (As you did) but when you add more FIELD, unique id ~> should change.

One good way to have special unique_id:

Use the first solution i said.

Or:

Create NEW table just to store YOUR COMBO:

New table will have ALL field and you will SET value for each (or you can write script to create it). ~> You have id for each combo ~> it same as unique id

You can create ALL THE POSSIBLE combo or just check and add when the NEW combo show up

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment