0

I am working on a website with Django.

I have created two models, one for photo and the other for a person.

class Photo(models.Model):    
    photo  = models.ImageField(upload_to = 'toto')
    description = models.CharField(_('Description'), max_length = 250)
    people = models.ManyToManyField('Person', related_name = _('Person'))

    def display_img(self):
        return u'<img src="%s" /> - %s - %s' % (self.photo, self.description, self.people )

    display_img.allow_tags = True

class Person(models.Model):
    name = models.CharField(_('Name'), max_length = 50)

In my mind what I want is to be able to tag people on a photo.

My problem is that I am able to create a new photo with the admin, but when I want to modify/view it (http://127.0.0.1:8000/admin/module/photo/1/) I get this error:

Django Version: 1.4.1
Exception Type: TypeError
Exception Value:    
filter() keywords must be strings
Exception Location: C:\Python26\Lib\site-packages\django\db\models\fields\related.py in get_query_set, line 543

and I am not able to understand it.

cezar
  • 11,616
  • 6
  • 48
  • 84
trnsnt
  • 674
  • 4
  • 15

2 Answers2

1

The full traceback would have been useful.

I expect the problem comes from the related_name attribute in your people field. You've marked this for translation, but that makes no sense: this is an attribute you use in your code, not something for public consumption. Take out the _() call.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you very much for your response, it solves the problem! You are a genious! My understanding of related_name was incorrect. I will go back to documentation. Next time I will give the traceback ;) Thank you very much – trnsnt Sep 13 '12 at 10:13
0

photo is object, but you want get this as str:

return u'<img src="%s" /> - %s - %s' % (self.photo, self.description, self.people )

You need url attr:

return u'<img src="%s" /> - %s - %s' % (self.photo.url, self.description, self.people )

My solution about people:

people = models.ManyToManyField(PersonModel, related_name = _('Persons'), verbose_name=_('Person'))
freylis
  • 814
  • 6
  • 15
  • Thank you very much for your response. It does not solve the problem, but it solves an other problem I have about the display of the image. Without it the image was not displayed. Thank you – trnsnt Sep 13 '12 at 10:09