0

I'm developping a Django interface and i have a problem changing name of an uploaded file.

Here's a part of my model

class Test(models.Model):
    var = models.IntegerField()
    var1 = models.ManyToManyField('OtherClass')

    file = models.FileField(upload_to='dir/%d',storage=MyFileSystemStorage())

As you can see, i have some manytomanyfield models and i want to be able to change the name of my file uploaded when i save it. I did it with my own method "MyfileSystemStorage", the problem is that i want to add var1 information "selected" in admin interface to the name of the file when i save.

Is it possible to do that and how? please if someone can help me i would be grateful ..

Thanks to all see ya

user1336204
  • 177
  • 1
  • 1
  • 8
  • similar: http://stackoverflow.com/questions/4840138/django-file-upload-filename-not-sticking – Hedde van der Heide May 24 '12 at 09:51
  • I don't see any similarities i'm not using any form only django admin upload and also one of the difficulty i have is to get the manytomanyfield information selected in admin interface. – user1336204 May 24 '12 at 10:05
  • The django admin upload is a form itself? You can overwrite it with your custom ModelAdmin https://docs.djangoproject.com/en/dev/ref/contrib/admin/ – Hedde van der Heide May 24 '12 at 10:13
  • Ok thanks, but is there a way to get selected value of a manytomanyfield ? and if yes how thanks again – user1336204 May 24 '12 at 10:47

1 Answers1

0
class ExampleForm(ModelForm):
    def save(self, commit=True):
        m = super(ExampleForm, self).save(commit=False)

        m2mvariable = self.cleaned_data['var1']
        # do stuff..

        if commit:
            m.save()
        return m

    class Meta:
        model = Test


class TestAdmin(admin.ModelAdmin):
    filter_horizontal = ('var1',)
    form = ExampleForm

admin.site.register(Test, TestAdmin)

For your other comment:

If you want a string representation you can create a unicode method for your model, for instance:

class Test(models.Model):
    #attrs...

    def __unicode__(self):
        return u"%s" % self.someattr

But you can do whatever you want with your m2m results, you can iterate through them:

for obj in results:
    print obj.attr
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • Thanks a lot!! so if i want now to get the path of "file" uploaded (file.path) and copy it to a new path/file.name and add the m2mvariable to the file.name how can i do this? i was thinking of "shutil" but don't know how to get the file.path and file.name of my Test model I'm kinda new to python and django so sorry with thoses "newbie" questions but thanks a lot again for the time spent answering me – user1336204 May 24 '12 at 12:20
  • Ok i did most of the job thanks again you helped me a lot!! one last thing is that m2m variable is a QuerySet type , when i print it i get "[]" , is it possible to get "variable" only as a string or do i have to parse this result? – user1336204 May 25 '12 at 08:26
  • You get a list (queryset) of objects. You can do whatever you want with them, I extended the above example. – Hedde van der Heide May 25 '12 at 08:29
  • All work except one LAST thing haha.. i explained before i'm trying to copy file uploaded to another path, since i'm doing this in the save method of my admin class i'm getting this error "'InMemoryUploadedFile' object has no attribute 'path'" when 'im trying to copy file in uploaded path to my other path , it's maybe coz the uploaded file isn't being copy to the path yet. So how can i deal with this i know that i'm asking a lot of things but it's very important thanks again – user1336204 May 25 '12 at 10:02
  • You can change or write your own backend for file handling. see the docs: https://docs.djangoproject.com/en/1.4/topics/http/file-uploads/ these are set in settings with a tuple – Hedde van der Heide May 25 '12 at 10:07
  • Sorry to come back for another newbie question but, if i have a charfield in my model, i want to change his value(with a string with my m2mvariable) in my save admin method. I tried this self.data['path_file'] = new_file_path where path_file is the charfield name of my model and new_file_path is the path i want to be affected to the charfield but it doesn't work, the charfield content is still the one added before save, last help if possible =) – user1336204 May 31 '12 at 08:25