Am Freitag, 25. Mai 2007 10:51 schrieb Thomas Güttler: > Hi, > > the upload_to argument to FileField evals strftime formatting. > > I would like to have the ID of the belonging row. > > Example: One MyObject has N attachments. > > class Attachment(models.Model): > file=models.FileField(upload_to="%(myobject_id)s") > myobject=models.ForeignKey(MyObject) > > > This way all attachments of one MyObjects are in the same directory.
Here is how I solved this: MyObject instances have N attachments: The files should be in myobjects/MYOBJECTID/... class MyAttachmentField(models.FileField): def get_directory_name(self): return os.path.normpath(os.path.join(self.upload_to, str(self.myobject_id))) def save_file(self, *args, **kwargs): new_data=args[0] self.myobject_id=new_data["myobject_id"] return models.FileField.save_file(self, *args, **kwargs) class MyAttachment(models.Model): def __str__(self): return '<%s %s %s>' % ( self.__class__.__name__, self.myobject.id, self.file) file=MyAttachmentFileField(upload_to="myobjects") myobject=models.ForeignKey(MyObject, edit_inline=models.STACKED) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---