小智 7
如果您不想显式调用 save() 方法,可以使用以下方法创建记录MyModel.objects.create(p1=v1, p2=v1, ...)
fruit = Fruit.objects.create(name='Apple')
# get fruit id
print(fruit.id)
Run Code Online (Sandbox Code Playgroud)
事实上,它是在" 编写你的第一个Django应用程序 "教程的第一部分中提到的.
正如"使用API"部分所述:
>>> from django.utils import timezone
>>> p = Poll(question="What's new?", pub_date=timezone.now())
# Save the object into the database. You have to call save() explicitly.
>>> p.save()
# Now it has an ID. Note that this might say "1L" instead of "1", depending
# on which database you're using. That's no biggie; it just means your
# database backend prefers to return integers as Python long integer
# objects.
>>> p.id
1
Run Code Online (Sandbox Code Playgroud)
本教程的第4部分介绍了如何使用表单以及如何使用用户提交的数据保存对象.
归档时间: |
|
查看次数: |
22907 次 |
最近记录: |