小编Tac*_*Tac的帖子

Django意外保存字符串元组

我正在更新 django 中的数据,但是字符串数据在保存在数据库中时变成了元组字符串。

@api_view(["POST"])
def cate_edit(req):
    if not req.user.is_staff:
        return HttpResponseNotFound()
    data=jsonload(req.body)
    if not has(data,["id","title","other_title","introduction"]):
        return HttpResponseForbidden()
    id=toNumber(data["id"])
    if id==None:
        return HttpResponseForbidden()
    if id==0:
        c=Category(
            title=data["title"],
            other_title=data["other_title"],
            introduction=data["introduction"]
        )
        c.save()
        return HttpResponse(c.id)
    else:
        c=get_object_or_404(Category,id=id)
        c.title = data["title"],
        c.other_title = data["other_title"],
        c.introduction = data["introduction"]
        c.save()
        return HttpResponse(c.id)
Run Code Online (Sandbox Code Playgroud)

问题发生在最后else,我可以确保数据是有效且正常的字典,例如 {'id': 1, 'title': '1', 'other_title': '2', 'introduction': '3'} 但是在此保存过程之后,数据库中的数据是

title: "('1',)"
other_title:"('2',)"
introduction: '3'
Run Code Online (Sandbox Code Playgroud)

介绍其实是对的。

此外,这是类别的模型

title: "('1',)"
other_title:"('2',)"
introduction: '3'
Run Code Online (Sandbox Code Playgroud)

谢谢

更新:使用 query and 很酷update,但为什么会发生上述情况?我曾经这样做过,但效果很好。

python django django-rest-framework

4
推荐指数
1
解决办法
574
查看次数

具有相同名称调用的非覆盖子类方法

class Animal{
    void eat(Animal animal){
        System.out.println("animal eats animal");
    }
}

public class Dog extends Animal{
    void eat(Dog dog){
        System.out.println("dog eats dog");
    }

    public static void main(String[] args) {
        Animal a = new Dog();
        Dog b = new Dog();
        a.eat(b);
        b.eat(b);
    }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,输出将是

动物吃动物
狗吃狗

为什么会这样?

java oop polymorphism inheritance

3
推荐指数
1
解决办法
97
查看次数

芹菜定期任务未在 Django 中运行

文件结构

proj/proj/
         celery.py
         (and other files)
    /sitesettings/
         tasks.py
         (and other files)
Run Code Online (Sandbox Code Playgroud)

芹菜.py

app = Celery('mooncake',broker_url = 'amqp://')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
Run Code Online (Sandbox Code Playgroud)

站点设置/任务.py

from __future__ import absolute_import, unicode_literals
from comma.models import Post
from mooncake.celery import app

app.conf.beat_schedule = {
'every-5-seconds': {
    'task': 'sitesettings.tasks.statisticsTag',
    'schedule': 5.0,
    'args': ()
},
}

@app.task
def statisticsTag():
    print(Post.objects.all()[0])
Run Code Online (Sandbox Code Playgroud)

并运行它

celery -A proj beat -l info
Run Code Online (Sandbox Code Playgroud)

把它拿出来

[2019-02-22 18:21:08,346: INFO/MainProcess] Scheduler: Sending due task every-5-seconds (sitesettings.tasks.statisticsTag)
Run Code Online (Sandbox Code Playgroud)

但没有进一步的输出。我曾经尝试在 proj/celery.py 中编写它,但它无法运行,因为我必须从另一个应用程序导入,它以“应用程序未加载”错误退出。所以我该怎么做?

python django celery django-celery celerybeat

2
推荐指数
1
解决办法
2188
查看次数