Python jsonfield 模块,JSONField() 实例源码

我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用jsonfield.JSONField()

项目:automated-reporting    作者:Waikato    | 项目源码 | 文件源码
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()
项目:news    作者:kuc2477    | 项目源码 | 文件源码
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