Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config Order Maintained #375

Merged
merged 8 commits into from
Jun 11, 2024
Merged

Config Order Maintained #375

merged 8 commits into from
Jun 11, 2024

Conversation

mexi-cano
Copy link
Collaborator

What (if anything) did you refactor?

  • Updated config model and serialization to maintain object order. Updated init config data to correct order.

@mexi-cano mexi-cano changed the title Refactor/config Config Order Maintained May 22, 2024
@mexi-cano mexi-cano changed the base branch from main to dev May 22, 2024 13:44
@CalebPena
Copy link
Collaborator

I think that you need to run python manage.py makemigrations and than migrate.

@mexi-cano
Copy link
Collaborator Author

Using an OrderedDict to return the data field as an OrderedDict instance did not work for me. I tried returning the ordered data as such but the approach did not work.

def ordered_data(self):
        return json.loads(json.dumps(self.data), object_pairs_hook=OrderedDict)

Instead I created a custom OrderedJSONField that inherits from JSONField and overrides the serialization and deserialization methods to handle ordered dictionaries. To achieve this, I made changes to the model, serializer, and created a custom field class:

Custom OrderedJSONField (fields.py):

I created a custom OrderedJSONField class that inherits from Django's JSONField.
In the from_db_value and to_python methods, I use json.loads with a custom object_pairs_hook that converts the deserialized JSON object into an OrderedDict. This ensures that the order of the keys is preserved when retrieving data from the database.
The get_prep_value method is used to serialize the JSON data before saving it to the database.

Model (models.py):

In the model definition, I replaced the default JSONField with our custom OrderedJSONField for the data field.
This ensures that the JSON data stored in the data field will maintain the order of the keys.

Serializer (serializers.py):

In the serializer, I specified the data field as an instance of OrderedJSONField.
This ensures that the serializer uses the custom field for serialization and deserialization of the JSON data.

@CalebPena

Copy link
Collaborator

@CalebPena CalebPena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but you forgot to run python manage.py makemigrations.

@CalebPena
Copy link
Collaborator

@mexi-cano Could you fix the merge conflicts?

@CalebPena CalebPena merged commit 6448203 into dev Jun 11, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants