Django 多对多显示值,而不是对象
我正在使用 Django 1.3,使用以下 2 个模型:
class Person(models.Model):
name = models.CharField(max_length=500)
def __unicode__(self):
return self.name
class Trial(models.Model):
person = models.ManyToManyField(Person, blank=True)
strId = str(id)
def __unicode__(self):
return self.strId
我试图将“name”的所有条目显示到视图,但我当前仅返回
只有当我查看页面源代码时我才能看到。否则它看起来就像一个空白的白屏。
观点如下:
def trial_entry(request, trial):
currentTrial = Trial.objects.get(id = 1)
personName = Trial.person
return HttpResponse(personName)
我显然在这里返回了错误的东西,但我已经寻找了另一种方法来做到这一点,但我发现似乎没有任何效果。这是我发现的返回除错误之外的任何内容的唯一方法,所以我认为这是最好的发布方式。
非常感谢
I'm working with Django 1.3, with the following 2 models:
class Person(models.Model):
name = models.CharField(max_length=500)
def __unicode__(self):
return self.name
class Trial(models.Model):
person = models.ManyToManyField(Person, blank=True)
strId = str(id)
def __unicode__(self):
return self.strId
I am trying to display all the entries of "name" to a view, but I am currently returning only <django.db.models.fields.related.ReverseManyRelatedObjectsDescriptor object at 0x1019b45d0>
Which I can only see when I view the Page Source. Otherwise it just looks like a blank white screen.
The view is as follows:
def trial_entry(request, trial):
currentTrial = Trial.objects.get(id = 1)
personName = Trial.person
return HttpResponse(personName)
I'm obviously returning the wrong thing here, but I've scoured for another way to do it and nothing I've found seems to work. This is about the only way I've found to return anything other than an error, so I figured that was the best thing to post.
Thanks very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些错误。首先,
personName
并未访问Trial
的任何实际实例,而您只是获取有关Trial< 中的
person
属性的信息/代码> 类。因此,您需要做的第一件事就是改变获取人员的方式。
你看出什么不同了吗?我在此视图中访问
currentTrial
,它是您正在处理的Trial
类的实例。到目前为止有意义吗?现在,
currentTrial.person
实际上是一个 ManyToMany 管理器,可以对其进行迭代,因为我们可以让很多人参与 Trial。所以我们应该调整变量名称以使其更有意义。您可以更进一步,只需将
currentTrial
发送到模板上下文,然后在模板中访问该试用人员,如下所示:希望有帮助!
There are a few mistakes here. First,
personName
is not accessing any actual instance ofTrial
, rather you are just getting information about theperson
attribute within theTrial
class.So the first thing you need to do is change how you fetch persons.
Do you see what's different? I access
currentTrial
, which is the instance of theTrial
class you are dealing with, within this view. Makes sense so far?Now,
currentTrial.person
is actually a ManyToMany manager, which can be iterated through, since we can have many persons on a Trial. So we should adjust the variable names to make a bit more sense.You could go one step further and simply send
currentTrial
to the template context, and then access that trials persons within your template like so:Hope that helps!
首先,Trial.person 是 Trial 模型本身(类)的引用,而不是其实例。您可能应该将其写为
currentTrial.person
或Trial.person
,具体取决于您感兴趣的试验。其次,您使用了多对多字段人的属性。这意味着您可以让多个人与试验相关联 - 更清楚地命名为“人”。
如果你真的指的是人,我建议更改名称,但你实际上会得到一个类似查询集的对象。您可以执行
person.all()
来获取全部人员,或者执行person.filter(something=value)
来获取与试验相关的人员子集。如果您的意思确实是“人”(与试验相关的单个人),那么您应该使用 ForeignKey 字段。这会将一个人与可以通过 currentTrial.person 访问的试验相关联。
祝你好运!
Firstly, Trial.person is a reference on the Trial model itself (the class), not an instance thereof. You should probably write it as
currentTrial.person
ortrial.person
depending on which trial you are interested in.Secondly, you've used a many-to-many field for the person attirbute. This means that you can have multiple people associated with a trial - it would be more clearly named, 'people'.
If you really do mean people I'd suggest changing the name, but you actually get a queryset-like object back. You can do
person.all()
to get them all, orperson.filter(something=value)
to get a subset of the people associated with the trial.If you really mean 'person' (a single preson associated with a trial) then you should use the ForeignKey field. This will associate a single person with the trial that can be accessed with currentTrial.person.
Good luck!