> I have a model for Thumbnail, but I cannot save it. It says: > AttributeError: 'Thumbnail' object has no attribute 'id'. > What is wrong? > > I use django 1.0-final-SVN-unknown > > thanks, Manuel > > Here is an example: > > >>> from portfolio.models import Photo, Thumbnail > >>> p = Photo.objects.all()[0] > >>> t = Thumbnail(p,'w300')
See below.. > >>> t.save() > > Traceback (most recent call last): > File "<console>", line 1, in <module> > File "/Users/manuel/Sites/mgh/django/db/models/base.py", line 307, > in save > self.save_base(force_insert=force_insert, force_update=force_update) > File "/Users/manuel/Sites/mgh/django/db/models/base.py", line 347, > in save_base > pk_val = self._get_pk_val(meta) > File "/Users/manuel/Sites/mgh/django/db/models/base.py", line 288, > in _get_pk_val > return getattr(self, meta.pk.attname) > AttributeError: 'Thumbnail' object has no attribute 'id' > > The models: > > from django.db import models > from django.utils.translation import ugettext as _ > import Image as PilImage > import ekin.settings as s > import os > > # Create your models here. > class Galery(models.Model): > title = models.CharField(_("Name"), max_length=200) > > class Photo(models.Model): > picture = models.ImageField(null=True, upload_to='./images/') > caption = models.CharField(_("Optional > caption"),max_length=100,null=True, blank=True) > item = models.ForeignKey(Galery) > > def get_thumbnail_url(self, string): > return Thumbnail(self, string).get_thumbnail() > > def __unicode__(self): > return unicode(self.picture) > > class Thumbnail(models.Model): > miniature_url = models.CharField(max_length=100) > item = models.ForeignKey(Photo) > x,y = None, None > def __init__(self, item, dimensions): > self.dimensions = dimensions > self.item = item > self.__process() > self.__make_thumbnail() You are overriding __init__ but not making sure to call the base class's __init__ method which does a whole bunch of stuff whenever a new instance of a Model class is created. This is most definitely causing your problem. It's best not to overload it with your own __init__. A better option is to hook into the post_init signal with a custom method and in that method do your process() and make_thumbnail() calls. See: - http://docs.djangoproject.com/en/dev//topics/signals/ - http://docs.djangoproject.com/en/dev/ref/signals/ -RD --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---