Wednesday, June 27, 2018

Re: better way to create a dictionary in Django model?

The way I work with json data in the admin is rather complicated. Here are a the steps I use - check the django documentation and Google for details.

As an example, assume you have a table Documents with a JSON field called metadata and other "normal" django fields. The metadata is a dictionary of key - value pairs. Note: the "value" can be a string, list, dictionary, integer, real, etc. It all depends on your needs and the field/widget you define for your field.

1. Create a DocumentAdminForm(forms. ModelForm)
2. Override the __init__ method in the DocumentAdminForm to create all of your JSON fields. I use the key as the name of the field. You will have field definitions of the form
self.fields[key] = forms.CharField(<normal parameters as in any django model>). You can use any field type - DateField, BooleanField, ModelChoiceField, ModelMultipleChoiceField, etc. For the last two fields, your value will be a list in the JSON data. You can also use any of the django or admin widgets for these fields - just add them to the field definition.
3. To initialize the field with data from your JSON field, use self.fields[key].initial = <value from your JSON dictionary for that key> in the __init__ method.
4. In django > 1.8, be sure to have fields='__all__' in the Meta section of the DocumentAdminForm
5. Override the clean method to validate the data you get back from the form. There will be an entry in the self.cleaned_data dictionary for each field in the form. You have to go through all the entries for your JSON keys, validate whether the data is good or not, save them into a metadata dictionary of the correct form for your JSON field, and remove that field from the self.cleaned_data dictionary. At the end, the self.cleaned_data should only have the fields that are defined in your model, and one dictionary for your metadata JSON object. If one of the JSON fields has bad data, then add a validation error to self.add_errors(key, validation_error) - still remove this field info from the self.cleaned_data. 

Create a DocumentAdmin(model.Admin) class. 
1. set the form = DocumentAdminForm
2. Define a fieldset just for the fields in your model (not the JSON field)
3. Override get_form to flatten the fieldsets:
    def get_form(self, request, obj=None, **kwargs):
        kwargs['fields'] = flatten_fieldsets(self.fieldsets)
        return super(DocumentAdmin, self).get_form(request, obj, **kwargs)

 4. Override get_fieldsets(self, request, obj=None) to add a fieldset definition for your keys in the JSON object to the fieldsets object you defined in step 2. Same keys used to define the fields in the form.

And that is about it! The admin page for your model will have a widget for each of your keys in your JSON data. I can't post the code I use, but this outline should give you enough pointers to look in the Django docs and get some examples from Google. 

Good luck!

Mark

On Wed, Jun 27, 2018 at 6:23 AM, prateek gupta <gupta607@gmail.com> wrote:
It has some limit- The configuration object can only be changed, there's no link for "add" (1)

On Wednesday, June 27, 2018 at 6:41:41 PM UTC+5:30, Egor Smolyakov wrote:
Better way it to use packages like this https://github.com/lazybird/django-solo

On 27 June 2018 at 15:18, prateek gupta <gupt...@gmail.com> wrote:
Hi All,

I am using Django 2.0.6 with Python 3.6 and MySql and created a model which maps the db tables to django.
In my database, I have a column 'config' of type json.
This column stores the data in a dictionary format like below- 
{"user_id": "a...@example.com", "password": "xyz"}
I need to make it editable via the Django like below screen shot-


I need your suggestion - what is the best way to achieve this?
I found a useful link for this purpose -how-to-store-a-dictionary-on-a-django-model but want to take your valuable guidance before proceeding so that I can implement with best practices.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/eeaba88f-136a-4e03-9599-b564a596bdf8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Kind Regards, Yehor Smoliakov.



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/44e2b155-e62a-4724-9cb5-32437ca471d6%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEqej2MOrdEy3UwbLgzj%2BNHP4A2wePCbPZep04kBTTsLUd869Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment