App Engine - 尝试设置模型属性值时出现问题

发布于 2024-07-15 22:33:46 字数 1006 浏览 5 评论 0原文

我对应用程序引擎还很陌生,我第一次尝试在应用程序引擎数据库中设置一些文本。

这是我的代码:

def setVenueIntroText(text):
  venue_obj = db.GqlQuery("SELECT * FROM Venue").get()
  venue_obj.intro_text = text     # Works if I comment out
  db.put(venue_obj)               # These two lines

这会引发某种异常 - 由于我的 django 1.02 设置,我无法说出它是什么。

好的,我在下面的答案中给出了代码,它在删除我的数据存储后起作用了,但我仍然不满意。

这是一个更新:

我已将代码修改为看起来对我有意义的内容。 当我调用 getVenueIntroText 时,它不会抱怨 - 顺便说一句,我在数据库中没有任何项目。

当我调用 setVenueIntroText 时,由于某种原因它不喜欢我正在做的事情 - 如果有人知道原因,我真的很想知道:)

这是我最新的尝试:

def getVenueIntroText():
  venue_info = ""
  venue_obj = db.GqlQuery("SELECT * FROM Venue").get()

  if venue_obj is not None:
      venue_info = venue_obj.intro_text

  return venue_info

def setVenueIntroText(text):
  venue_obj = db.GqlQuery("SELECT * FROM Venue").get()
  if venue_obj is None:
     venue_obj = Venue(intro_text = text)
  else:
     venue_obj.intro_text = text

  db.put(venue_obj)

I'm pretty new to app engine, and I'm trying to set a bit of text into the app engine database for the first time.

Here's my code:

def setVenueIntroText(text):
  venue_obj = db.GqlQuery("SELECT * FROM Venue").get()
  venue_obj.intro_text = text     # Works if I comment out
  db.put(venue_obj)               # These two lines

This throws some sort of exception - I can't tell what it is though because of my django 1.02 setup.

Ok, I gave the code in the answer below a go, and it worked after deleting my datastores, but I'm still not satisfied.

Here's an update:

I've modified my code to something that looks like it makes sense to me. The getVenueIntroText doesn't complain when I call it - I haven't got any items in the database btw.

When I call setVenueIntroText, it doesn't like what I'm doing for some reason - if someone knows the reason why, I'd really like to know :)

Here's my latest attempt:

def getVenueIntroText():
  venue_info = ""
  venue_obj = db.GqlQuery("SELECT * FROM Venue").get()

  if venue_obj is not None:
      venue_info = venue_obj.intro_text

  return venue_info

def setVenueIntroText(text):
  venue_obj = db.GqlQuery("SELECT * FROM Venue").get()
  if venue_obj is None:
     venue_obj = Venue(intro_text = text)
  else:
     venue_obj.intro_text = text

  db.put(venue_obj)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽你眉间 2024-07-22 22:33:46

我认为这应该有效:

def setVenueIntroText(text):
  query = db.GqlQuery("SELECT * FROM Venue")
  for result in query:
    result.intro_text = text
    db.put(result)

I think this should work:

def setVenueIntroText(text):
  query = db.GqlQuery("SELECT * FROM Venue")
  for result in query:
    result.intro_text = text
    db.put(result)
殤城〤 2024-07-22 22:33:46

我认为主要问题是我看不到错误消息 - 我真的很愚蠢,我忘记将 DEBUG = True 放入我的settings.py

事实证明我需要在我的 StringProperty 中使用 multiline=True

Django 正在捕获我的异常为我。

I think the main problem was that I couldn't see the error messages - really stupid of me, I forgot to put DEBUG = True in my settings.py

It turns out I needed a multiline=True in my StringProperty

Django is catching my exceptions for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文