I'm trying to access the current instance of ScreenShot related to the
current instance of WebProject, in order to get the slug field of
WebProject to use as a upload directory but I don't think I can
because there's no primary key in the db yet. I need to call
change_upload_to when the instance of WebProject tries to save because
it saves before the instance of ScreenShot i.e., before slug is
obtained to append to the upload_to directory. Are you suggesting I
should just add the ImageField to the WebProject class? I don't want
to have a fixed number of ImageFields for WebProject, if there's a way
to do that without using another class that would work. Can I access
the save method of ScreenShot from WebProject? Should I just add
another slug field to ScreenShot?
class ScreenShotInline(admin.StackedInline):
model = ScreenShot
extra = 3
class WebProjectAdmin(admin.ModelAdmin):
prepopulated_fields = {'slug': ('title',)}
inlines = [ScreenShotInline]
On Nov 25, 9:46 am, Preston Holmes <[email protected]> wrote:
> I'm a bit confused on a few points here. The upload_to field
> attribute should be atomic to the imagefield - it doesn't rely on
> related models.
>
> your change_upload_to creates a new screenshot - but doesn't connect
> it to the current WebProject instance. Is that what you are trying to
> do?
>
> s = ScreenShot(project=instance)
>
> Is there a reason your WebProject is not defined when you create a
> screenshot in your view?
>
> Something about this seems like it is making it more complicated than
> it needs to be. but details are lacking t
>
> -Preston
>
> On Nov 24, 5:52 pm, neridaj <[email protected]> wrote:
>
> > Hello,
>
> > I'm trying to change the upload_to attribute of an ImageField so that
> > it is in the format "web_projects/year/slug/". The problem I am having
> > is that the ForeignKey model is getting saved before the related
> > ScreenShot model is able to call it's save method to change upload_to.
> > I'm trying to use a pre_save signal to reverse the order of saving but
> > I'm not sure how to access the current instance of the ScreenShot
> > model to call it's save method. Thanks for any suggestions.
>
> > class ScreenShot(models.Model):
> > project = models.ForeignKey(WebProject)
> > image = models.ImageField(upload_to="web_projects/%Y/")
> > page_title = models.CharField(max_length=250, help_text="Maximum 250
> > characters.")
>
> > def __unicode__(self):
> > return self.page_title
>
> > def save(self):
> > self.image.upload_to="web_projects/%Y/"+self.project.slug
> > super(ScreenShot, self).save()
>
> > def change_upload_to(sender, **kwargs):
> > s = ScreenShot() # not sure how to access current instance
> > s.save()
>
> > pre_save.connect(change_upload_to, sender=WebProject)
--
You received this message because you are subscribed to the Google Groups
"Django users" 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-users?hl=en.