Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dict as a model field

I want to add a dict field to a model, display the dict on the admin, and be able to edit it from the admin.

For example, I have a relationship

dict = { 'sister' : rel_score, 'mother' : rel_score, 'father': rel_score}

where rel_score (default = 0) is the score for each relationship. I want to store this dict to my model and display it in admin, so that I can assign these rel_score for each relationship on my admin.

Also, any example of how to assign scores (prioritize) to different elements, and return values according to these assigned scores would be really helpful.

like image 577
user_2000 Avatar asked Feb 12 '13 16:02

user_2000


People also ask

What is a model field in Python?

If you break it down, a model field provides a way to take a normal Python object – string, boolean, datetime, or something more complex like Hand – and convert it to and from a format that is useful when dealing with the database.

What is model_to_Dict ()?

Here is the code for model_to_dict () that was created. Once the conversion code of the a model is unified into just one line as shown below, repetitive and annoying coding for conversion is no longer needed. Since the parent class is created and the most job in the inherited child class is just to specify only the fields to be converted.

What is a dictfield in Python?

DictField is basically a dictionary field that validates the input against a dictionary of objects. child – A field instance that should be used for validating the values in the dictionary.

How to write your own custom model fields in Django?

Also, you can easily write your own custom model fields. Technically, these models are defined in django.db.models.fields, but for convenience they’re imported into django.db.models; the standard convention is to use from django.db import models and refer to fields as models.<Foo>Field. The following arguments are available to all field types.


2 Answers

It's not really possible to have a DictField since the underlying database won't support that type of data structure.

You could have a RelationshipScore model to store your relationships and a ForeignKey from it to your existing model (I'll use user as an example). eg:

Class RelationshipScore(models.Model):
    user = models.ForeignKey(User)

    relationship = models.CharField(max_length=50, primary_key=True)
    score = models.IntegerField()

You could then add functions to your existing model to get the values:

class User(models.Model):
    def get_relationship(self, rel):
        return RelationshipScore.objects.get(relationship=rel, user=self)
    def get_lowest_relationship(self):
        return RelationshipScore.objects.filter(user=self).order_by("-score")[0]
like image 87
OllyTheNinja Avatar answered Oct 20 '22 11:10

OllyTheNinja


As of Django 1.8, postgres users also have the option of using an HStore field.

A field for storing mappings of strings to strings. The Python data type used is a dict.
like image 38
shacker Avatar answered Oct 20 '22 09:10

shacker