[英]Django-nonrel on Appengine
class BillList(models.Model):
username = models.ForeignKey(User)
billno = models.CharField(max_length=15)
class OrderDetails(models.Model):
billno = models.ForeignKey(BillList)
orderdetails = models.TextField()
User
is the one within django.contrib.auth.models
. User
是django.contrib.auth.models
User
。
I need to retreive all billno
of a particular user. 我需要检索特定用户的所有
billno
。 How do I go about doing this simple query in Django-nonrel on Appengine? 如何在Appengine的Django-nonrel中执行此简单查询?
If I do this: 如果我这样做:
iq = User.objects.filter(username = "name1")
BillList.objects.filter(username = iq)
Then I get an error: DatabaseError: Subqueries are not supported.
然后我得到一个错误:
DatabaseError: Subqueries are not supported.
If I try this straight away BillList.objects.filter(username = "restaurant1")
, then ValueError: invalid literal for long() with base 10: 'restaurant1'
如果我立即尝试使用
BillList.objects.filter(username = "restaurant1")
,则ValueError: invalid literal for long() with base 10: 'restaurant1'
I'm sure it must be possible to go about doing this simple query! 我敢肯定一定有可能去做这个简单的查询! Any workarounds?
任何解决方法?
The others are correct. 其他是正确的。 But, there may be a fundamental problem here with your understanding of the
ForeignKey
. 但是,您对
ForeignKey
的理解可能在这里存在根本问题。 For example: 例如:
username = models.ForeignKey(User)
That's not really a "username" at all. 根本不是一个“用户名”。 It is a user object.
它是一个用户对象。 More understandable would be something like:
更容易理解的是:
user = models.ForeignKey(User)
The User
object is what has the username
property. User
对象是具有username
属性的对象。 So to get a person's username, you would use" 因此,要获取某人的用户名,您可以使用“
BillList.objects.get(billno = 12345).user.username
Then, your queries become: 然后,您的查询变为:
iq = User.objects.get(username = "name1")
my_list = BillList.objects.all().filter(user = iq)
Or, more directly: 或者,更直接地:
my_list = iq.billist_set.all()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.