Python jsonfield.JSONField() Examples

The following are code examples for showing how to use jsonfield.JSONField(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: automated-reporting   Author: Waikato   File: models.py    GNU General Public License v3.0 6 votes vote down vote up
def write_last_parameter(user, param, value):
    """
    Saves the parameter value for the user.

    :param user: the user to save the parameter for
    :type user: User
    :param param: the name of the parameter to save
    :type param: str
    :param value: the value to store
    :type value: str
    """
    params = LastParameters.objects.all().filter(user=user)
    if len(params) == 0:
        p = LastParameters()
        p.user = user
        p.parameters = JSONField()
        jparams = json.loads("{}")
    else:
        p = params[0]
        jparams = json.loads(p.parameters)

    jparams[param] = value
    p.parameters = json.dumps(jparams)
    p.save() 
Example 2
Project: news   Author: kuc2477   File: django.py    MIT License 5 votes vote down vote up
def create_schedule_abc(user_model=None):
    """Abstract base schedule model factory.

    :param user_model: User model to use as schedule owners.
    :type user_model: :class:`~news.models.AbstractModel`
        implemenatation
    :returns: A abstract base schedule model.
    :rtype: Abstract base django model of
        :class:`~news.models.AbstractSchedule` implementation.

    """
    user_model = user_model or settings.AUTH_USER_MODEL

    class AbstractBaseSchedule(models.Model, AbstractSchedule):
        owner = models.ForeignKey(
            user_model, related_name='schedules',
            db_index=True, blank=True, null=True,
        )

        url = models.URLField()
        cycle = models.IntegerField(default=DEFAULT_SCHEDULE_CYCLE)
        enabled = models.BooleanField(default=False)
        type = models.CharField(max_length=SCHEDULE_TYPE_MAX_LENGTH,
                                default=DEFAULT_SCHEDULE_TYPE)
        options = JSONField(default=DEFAULT_OPTIONS)

        class Meta:
            abstract = True
            unique_together = (('owner', 'url'),)

    return AbstractBaseSchedule 
Example 3
Project: kpi   Author: kobotoolbox   File: asset.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def optimize_queryset_for_list(queryset):
        ''' Used by serializers to improve performance when listing assets '''
        queryset = queryset.defer(
            # Avoid pulling these `JSONField`s from the database because:
            #   * they are stored as plain text, and just deserializing them
            #     to Python objects is CPU-intensive;
            #   * they are often huge;
            #   * we don't need them for list views.
            'content', 'report_styles'
        ).select_related(
            'owner__username',
        ).prefetch_related(
            # We previously prefetched `permissions__content_object`, but that
            # actually pulled the entirety of each permission's linked asset
            # from the database! For now, the solution is to remove
            # `content_object` here *and* from
            # `ObjectPermissionNestedSerializer`.
            'permissions__permission',
            'permissions__user',
            # `Prefetch(..., to_attr='prefetched_list')` stores the prefetched
            # related objects in a list (`prefetched_list`) that we can use in
            # other methods to avoid additional queries; see:
            # https://docs.djangoproject.com/en/1.8/ref/models/querysets/#prefetch-objects
            Prefetch('tags', to_attr='prefetched_tags'),
            Prefetch(
                'asset_versions',
                queryset=AssetVersion.objects.order_by(
                    '-date_modified'
                ).only('uid', 'asset', 'date_modified', 'deployed'),
                to_attr='prefetched_latest_versions',
            ),
        )
        return queryset