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
There are a few mistakes here. First,
personNameis not accessing any actual instance ofTrial, rather you are just getting information about thepersonattribute within theTrialclass.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 theTrialclass you are dealing with, within this view. Makes sense so far?Now,
currentTrial.personis 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
currentTrialto the template context, and then access that trials persons within your template like so:Hope that helps!