简体   繁体   中英

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 .

I need to retreive all billno of a particular user. How do I go about doing this simple query in Django-nonrel on Appengine?

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.

If I try this straight away BillList.objects.filter(username = "restaurant1") , then 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 . 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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM