English 中文(简体)
django 查询设定了特定领域的价值,包括用户概况
原标题:django query set values() specific fields to include user profile

I have 2 django models like this:

class UserProfile(models.Model):
    user             = models.OneToOneField(User)
    organisation     = models.CharField(max_length=200)

class Submission(models.Model):
    user             = models.ForeignKey(User)
    date_submission  = models.DateTimeField(db_index=True, default=datetime.now())
    date_published   = models.DateTimeField(db_index=True, null=True)
    status           = models.CharField(db_index=True, max_length=4000)
    logfile          = models.TextField(db_index=False)

如果每个提交物体都由正常的django用户拥有,每个用户都有一个用户查询文件,使用AUTH_PROFILE。 教育部采取正常方式。

这符合你的期望,即能够看到用户档案物体的组织领域:

Submission.objects.all()[0].user.userprofile.organisation

如果想将提交清单(出口到json)序号,通常是指:

Submission.objects.all().values()
# or for more control of fields
Submission.objects.all().values( status ,  date_submission )
# to traverse a relationship.. get info from the User object
Submission.objects.all().values( user__username )

工作罚款。 我的问题是:

Submission.objects.all().values( user__userprofile__organisation )

raise FieldError("Invalid field name:  %s " % name)
django.core.exceptions.FieldError: Invalid field name:  user__userprofile__organisation 

so it seems that the UserProfile is a special case . This was discussed here: Django query : Call values() on user__userprofile

但是,解决办法不利于我(我很相信)。

这是否是对价值观方法的一种限制? 任何人都知道如何获得同样的价值产出,但能够推翻用户档案模式?

感谢任何想法

- i

最佳回答

如果将Django的升级为1.4版,这个问题已经解决,那么Kingo的评论表明,1.3.1也会ok。

现在可以:

query_set = Submission.objects.filter()
query_set.values( user__userprofile__organisation )
[{ user__userprofile__organisation : u organisation test 1 }]

che

- i

问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签