本文整理匯總了Python中models.Category.get_or_insert方法的典型用法代碼示例。如果您正苦於以下問題:Python Category.get_or_insert方法的具體用法?Python Category.get_or_insert怎麽用?Python Category.get_or_insert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類models.Category
的用法示例。
在下文中一共展示了Category.get_or_insert方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create
# 需要導入模塊: from models import Category [as 別名]
# 或者: from models.Category import get_or_insert [as 別名]
def create(request):
if not admin():
return HttpResponseRedirect(users.create_login_url('/blogs'))
form = BlogForm(request.POST)
form.fields['category'].choices = [('', '請選擇')] + [(category.key(), category.name) for category in Category.all()]
if form.is_valid():
blog = Blog()
blog.title = form.cleaned_data['title']
blog.content = form.cleaned_data['content']
if form.cleaned_data['category']:
blog.category = Category.get(form.cleaned_data['category'])
elif form.cleaned_data['user_tag']:
blog.category = Category.get_or_insert(form.cleaned_data['user_tag'], name=form.cleaned_data['user_tag'])
blog.create()
return HttpResponseRedirect('/blogs')
else:
return new(request)
示例2: create
# 需要導入模塊: from models import Category [as 別名]
# 或者: from models.Category import get_or_insert [as 別名]
def create(request):
if not admin():
return HttpResponseRedirect(users.create_login_url("/blogs"))
form = BlogForm(request.POST)
form.fields["category"].choices = [("", "請選擇")] + [(category.key(), category.name) for category in Category.all()]
if form.is_valid():
blog = Blog()
blog.title = form.cleaned_data["title"]
blog.content = form.cleaned_data["content"]
if form.cleaned_data["category"]:
blog.category = Category.get(form.cleaned_data["category"])
elif form.cleaned_data["user_tag"]:
blog.category = Category.get_or_insert(form.cleaned_data["user_tag"], name=form.cleaned_data["user_tag"])
blog.create()
return HttpResponseRedirect("/blogs")
else:
return new(request)
示例3: update
# 需要導入模塊: from models import Category [as 別名]
# 或者: from models.Category import get_or_insert [as 別名]
def update(request, key):
if not admin():
return HttpResponseRedirect(users.create_login_url('/blogs'))
form = BlogForm(request.POST)
form.fields['category'].choices = [('', '請選擇')] + [(category.key(), category.name) for category in Category.all()]
if form.is_valid():
blog = Blog.get(key)
blog_category_db=blog.category
blog.title = form.cleaned_data['title']
blog.content = form.cleaned_data['content']
if form.cleaned_data['category']:
blog.category = Category.get(form.cleaned_data['category'])
elif form.cleaned_data['user_tag']:
blog.category = Category.get_or_insert(form.cleaned_data['user_tag'], name=form.cleaned_data['user_tag'])
else:
blog.category = None
blog.put()
if blog_category_db and blog_category_db.blog_set.count() == 0:
blog_category_db.delete()
return HttpResponseRedirect('/blogs')
else:
return edit(request, key)