-3

我的应用程序在页面上有几个按钮。目前,这些按钮会进行 ajax 调用并在 views.py 中启动一个函数。该函数运行并向用户返回一些数据。我现在想要运行并更改模型中的对象。我该怎么做呢?

模型.py

class Stream(models.Model):
    step = models.IntegerField(default=0)
    ...

谢谢

4

1 回答 1

1

请参阅django 应用教程。您可以创建或修改模型对象,如下所示

from myapp.models import Stream

#To create object
Stream.objects.create(
                        city = 'Chennai',
                        ...
                     )

#To get a single object with a unique value
Stream.objects.get(id = 1)

#To update retrieved object
some_variable = Stream.objects.get(id = 1)
some_variable.city = 'Chennai'
some_variable.save()

#To filter objects with given value
Stream.objects.filter(city = 'Chennai')

#To update filtered objects
Stream.objects.filter(city = 'Chennai').update(country = 'India')
于 2013-08-16T04:39:49.070 回答