- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在已成功发送消息后更新已数据库保存的对象。它调用序列化器类的 update() 方法来实现此目的。这是将更新其实例的模型:
class SMSMessages(models.Model):
sms_number_to = models.CharField(max_length=14)
sms_content = models.CharField(max_length=160)
sending_user = models.ForeignKey("SMSUser", on_delete=models.PROTECT, related_name="user_that_sent")
sent_date = models.DateTimeField(auto_now=True)
delivery_status = models.BooleanField(default=False)
class Meta:
verbose_name_plural = "SMSMessages"
def __str__(self):
return str(self.sending_user)
这是我正在使用的序列化器类:
class SMSMessagesSerializer(serializers.ModelSerializer):
"""
A class for serializing the SMSMessages model's data. It sub-classes the
ModelSerializer class from serializer's module.
"""
class Meta:
model = SMSMessages
fields = '__all__'
def update(self, instance, validated_data):
"""
This method is used to update an instance of the SMSMessages's delivery_status attribute.
It get's the value for delivery_status from the input parameter, updates the specific instance
of the SMSMessagesSerializer, saves that instance and returns it.
"""
instance = self.get_object()
instance.delivery_status = validated_data.get('delivery_status', instance.delivery_status)
instance.save()
return instance
这是具有 POST 方法的 APIView 类,如果消息发送成功,该方法将更新:
class SMSView(APIView):
"""
This class is responsible for all the method operations of an sms. It provides implementations for the GET, POST, and OPTIONS methods.
Each method provides it's own description.
"""
serializer_class = SMSMessagesSerializer
def get(self, request):
"""
This method is used to GET all created instance of the SMSMessages class that are saved in the db.
"""
queryset = SMSMessages.objects.filter(sending_user=request.user)
while queryset:
return Response(
data={
queryset.values()
},
status=status.HTTP_200_OK,
content_type="application/json"
)
else:
return Response(
data={
"no sms has been sent"
},
status=status.HTTP_404_NOT_FOUND,
content_type="application/json"
)
def post(self, request):
"""
This method is used to create an instance of the SMSMessages indirectly by using the SMSMessagesSerializer.
If that is valid it will be passed to the sender() method from the notification.sender module. The serializer
will be saved, aka the object will be saved to the database, and then the sender() is called. It will run three
times before it gives up and fails. Once that returns a True value the instance will be called, aka the object
will be saved to the database, with a delivery_status value of True.
"""
sms_messages_serializer = SMSMessagesSerializer(
data={
"sms_number_to": request.data.get("sms_number_to"),
"sms_content": request.data.get("sms_content"),
"sending_user": request.data.get("sending_user")
}
)
permission_classes = (permissions.IsAuthenticated)
if sms_messages_serializer.is_valid():
data_to_send = {
"number": sms_messages_serializer.validated_data[
"sms_number_to"
],
"msg_text": sms_messages_serializer.validated_data[
"sms_content"
]
}
sms_messages_serializer.save()
# TODO refactor this into it's own function
max_retry = 0
resp = Response()
while max_retry < 3:
max_retry += 1
status_flag, status_response = sender(data_to_send)
if not status_flag:
resp = Response(
data={
"error": f"{status_response.text}"
},
status=status_response.status_code,
content_type="application/json"
)
else:
sms_messages_serializer.update(
data={
"delivery_status": True
},
partial=True
)
resp = Response(
data={
"success": f"{status_response.json()}"
},
headers=status_response.headers,
status=status_response.status_code,
content_type="application/json"
)
return resp
else:
resp = Response(
data={
"error": "unable to send sms"
},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
content_type="application/json"
)
return resp
它将通过以下 urlpattern 访问:
urlpatterns = [
path('sendsms/', SMSView.as_view(), name="send_sms"),
path('viewsms/', SMSView.as_view(), name="view_sms"),
]
但没有使用 python
成功更新实例它抛出类型错误:serializer.update()得到了意外的关键字参数'data'。
sms_messages_serializer.update(data={"delivery_status": True}partial=True)
我尝试更改 update
在 SMSMessagesSerializer 类中无济于事,并尝试重命名 data
属性为validated_data
或value
但仍然不起作用。我需要它在发送短信后将delivery_status更新为True,我怎样才能做到这一点?
最佳答案
对于任何可能遇到此问题的人,我通过将 post() 方法中的 sms_messages_serializer.update()
方法更改为:
sms_messages_serializer.update(sms_object,{"delivery_status": True})
其中sms_object=sms_object = sms_messages_serializer.save()
,表示数据经过验证并保存后的sms_messages_serializer实例。
不需要data
属性或partial=True
关于python - update() 抛出 TypeError : serializer. update() 得到意外的关键字参数 'data',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58575057/
命令 npm update 有什么区别和包裹npm-check-updates ?使用后者是否完全安全? 执行后好像是npm update并非所有软件包都已更新,因此似乎不完整。许多其他 popula
我有使用 ExpressJS 和 ORM Sequelize 的 API。我正在尝试使用 Sequelize 中的 update() 方法进行更新。默认情况下,it 方法将返回更新的行数。但我希望结果
关于如何更新 rubygems 有点困惑。过程不断变化(或者至少我从互联网上得到了相互矛盾的信息)。 $ gem outdated rubygems-update (1.8.10 < 1.8.11
我正在使用 webpack-dev-server处于开发模式( watch )。每次服务器重新加载时,一些 json 和 js 文件都会挤满我的构建目录,如下所示:'hash'.hot-update.
Mamp Pro 的当前版本是 5.04 (15996)。可用更新窗口显示“Mamp 5.0.0 > 5.1。更新失败,并显示一条消息:错误:无法验证更新。请确保您使用的是安全网络,然后重试。” 更新
我想在浏览量增加时更新时间戳“lastpageview_at”。我想我已经接近了,但我总是遇到语法错误,有人知道为什么或有其他解决方案吗? 我的触发器: CREATE TRIGGER Update_l
我正在执行 SELECT ... FOR UPDATE 以锁定一条记录,然后进行一些计算,然后进行实际的 UPDATE。我正在处理 InnoDB 数据库。 但是计算可能会以我不想执行 UPDATE 的
我需要在表更新时进行一些更新和插入以强制执行正确的数据。将 UPDATE 语句放入触发器中会导致某种“循环”吗? 谢谢! 最佳答案 更新触发器中的目标表将使触发器再次触发。 您可以使用 TRIGGER
这是我的布局 当我点击链接更新时,该链接应该打开和关闭renderComment bool
我有一个包含两件事的 Angular 范围: 一个包含 10k 行的巨型表格,需要一秒钟才能渲染 一些小的额外信息位于固定的覆盖标题栏中 根据您向下滚动页面/表格的距离,我必须更新标题中的小信息位之一
标题几乎已经说明了一切。 IF NEW.variance <> 0 THEN (kill update) END IF 这可能吗? 最佳答案 查看手册 (http://dev.mysql.com/do
我有几个表,我想强制执行版本控制,并且有一个生效日期和生效日期。每当应用程序或用户向该表写入更新时,我希望它重定向到两个全新的命令:更新目标记录,以便 EFFECTIVE_TO 日期填充当前日期和时间
我正在使用 Shopware,一件奇怪的事情让我抓狂 :( 所以我将首先解释问题是什么。 除了普通商品外,还有多种款式的商品,例如不同尺码的衬衫。这是 XS、S、M、L 和/或不同颜色的同一商品……但
寻求帮助制作 mysql 触发器。我当前的代码无法按预期工作。我想做的是,如果表A中的字段A被修改,则将字段A复制到表A中的字段B。 当前代码如下所示: BEGIN IF new.set_id=301
以下查询(来自此处Postgres SQL SELECT and UPDATE behaving differently) update fromemailaddress set call =
我想使用 D3 使用以下数据创建一个列表: var dataSet = [ { label: 'a', value: 10}, { label: 'b', value: 20},
哪个更好,先进行选择,然后进行更新。或者更确切地说,像这样合而为一: UPDATE items set status = 'NEW' where itemid in (1,2,3,
对于 eloquent model events,updating 和 updated 之间有什么区别? ? 我的猜测是 updating 在模型更新之前触发,而 updated 在模型更新之后触发。
我有一个对象数组(我们称之为arr)。在我的组件输入之一的 (change) 方法中,我修改了这些对象的属性之一,但在 View (*ngFor) 中没有任何变化。我读到 Angular2 变化检测不
我正在尝试使用 d3.js 构建水平日历时间线。主要目标是突出显示用户的假期和假期。 http://jsbin.com/ceperavu/2/edit?css,js,output 我首先从“开始”日期
我是一名优秀的程序员,十分优秀!