我的模型中有一个JSONField,当我尝试在django管理页面的json中将0保存为值时,它将值保存为null。如何将零保存为值?Django版本:2.1.7
Django管理页面:
我的JSONField:
lecturer_scores = JSONField(
verbose_name=_("Lecturer Score"),
validators=[validate_lecturer_score],
default=dict,
blank=True
)
Run Code Online (Sandbox Code Playgroud)
我的输入:
{
"score 1": 0,
"score 2": 5
}
Run Code Online (Sandbox Code Playgroud)
它保存如下:
{
"score 1": null,
"score 2": 5
}
Run Code Online (Sandbox Code Playgroud)
验证器:
from django.core.validators import validate_integer
from django.core.exceptions import ValidationError
def validate_lecturer_score(value):
for k, v in value.items():
if v:
validate_integer(v)
for value in value.values():
if value:
if value < 0 or value > 100:
raise ValidationError(_("Lecturer score for user's participance rate calculation should be between 0 and 100."))
Run Code Online (Sandbox Code Playgroud) 如何为我创建的新用户设置默认组?我没有创建用户的自定义模型,我的django版本是1.11.
如何course_code在此QuerySet中获取值?
<QuerySet [{'course_code': 11}]>
Run Code Online (Sandbox Code Playgroud) 当我尝试将分页添加到我的页面时,它给了我错误object of type 'RawQuerySet' has no len()
视图:
class StudentmessageListView(ListView, LoginRequiredMixin):
login_url = '/login/'
redirect_field_name = 'redirect_to'
template_name = 'student_messagesall.html'
context_object_name = 'messages_all'
model = Message
paginate_by = 3
def get_queryset(self):
return Message.objects.raw('SELECT * FROM ertaapp_message where to_prof_id=%s ORDER BY create_date DESC',[self.request.user.id])
def get_context_data(self, **kwargs):
context = super(StudentmessageListView, self).get_context_data(**kwargs)
context['reps'] = ReplyMessage.objects.raw('SELECT * FROM ertaapp_replymessage')
return context
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我使用过,truncatechars但是如果文本包含html标签,它将为我显示该标签。
{{content.text|safe|truncatechars:140}}
Run Code Online (Sandbox Code Playgroud)
例如显示如下内容:
<p>hi</p>
Run Code Online (Sandbox Code Playgroud)
我要删除p标签。