我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用jsonfield.JSONField()。
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()
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