On Mon, May 4, 2009 at 1:26 PM, skam <[email protected]> wrote:
> > I am using inline formsets to save multiple "Photo" models related > with "Company" model, but "Photo" instance's pk is set to None. I need > it to set the upload directory and filename using the pk value: > > Sample model: > > def get_photo_image_path(instance, filename): > filename = '%s%s' % (instance.id, os.path.splitext(filename)[1]) > # TypeError is raised here because instance.id is None > return u'companies/photos/%s/%s/%s' % (instance.id % 1000, > instance.id, filename) > > class Photo(models.Model): > company = models.ForeignKey(Company) > image = models.ImageField(upload_to=get_companyphoto_image_path) > description = models.CharField(blank=True, max_length=160) > > Sample formset: > > PhotoFormSet = inlineformset_factory(Company, Photo, can_order=False) > > Used as described in documentation with the latest Django svn > revision. The same code was running well on Django 1.0.2. I don't understand how this could have worked under 1.0.2 (and don't have time to test at the moment) since the doc for upload to has always included this note: In most cases, this object will not have been saved to the database yet, so if it uses the default AutoField, *it might not yet have a value for its primary key field*. (from: http://docs.djangoproject.com/en/1.0/ref/models/fields/#filefield). It's never been expected that you can use the pk value reliably within a callable upload_to. Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---
