[Django] #30811: Django throws 'UnicodeEncodeError' when using Cyrillic symbols to override field upload_to.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Django] #30811: Django throws 'UnicodeEncodeError' when using Cyrillic symbols to override field upload_to.

Django
#30811: Django throws 'UnicodeEncodeError' when using Cyrillic symbols to override
field upload_to.
-------------------------------------+-------------------------------------
               Reporter:  persik-    |          Owner:  persik-dev
  dev                                |
                   Type:             |         Status:  assigned
  Uncategorized                      |
              Component:  Database   |        Version:  2.2
  layer (models, ORM)                |       Keywords:  UnicodeEncodeError,
               Severity:  Normal     |  Cyrillic, upload_to, File,
           Triage Stage:             |  FileField
  Unreviewed                         |      Has patch:  0
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 So, I have a ''Book'' model, that contains some fields. But most important
 are:
 **'title' as 'CharField'**
 **'pdf', 'epub', 'fb2' as 'FileField'**
 **'image' as 'ImageField'**
 we will return to they in just a second...
 My ''Book'' model also has overrided ''clean'' method. In that method I
 assign my ''title'' field value as ''upload_to'' value.

 {{{
 uploading_files = [
     self.image,
     self.pdf,
     self.fb2,
     self.epub
 ]
 for file_ in uploading_files:
     file_.field.upload_to = self.title
 }}}
 Here is where bug hided. If I set ''title'' value to "Колобок", for
 example, it throws me an ''UnicodeEncodeError''. This thing happends only
 when i first create model.
 If I firstly will set ''title'' value as "Kolobok", and later rename it to
 "Колобок", I have no error.

 ----

 I already some kind of "fixed" this.
 Endpoint of thrown exception was **...\django\db\models\fields\files.py**.
 After fork from your Github, I followed this path, and find function that
 throws that error, that function was ''generate_filename'' in
 ''FileField'' class, line 305. After I added small correct...

 {{{
 try:
     dirname = datetime.datetime.now().strftime(self.upload_to)
 except UnicodeEncodeError:
     dirname = self.upload_to
 }}}

 ... all works fine now, and I can use Cyrillic symbols as my ''title'' and
 folder name in ''upload_to'' attribute.

--
Ticket URL: <https://code.djangoproject.com/ticket/30811>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/053.e445a74ead5d114b26d91bf3289025e5%40djangoproject.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #30811: Django throws 'UnicodeEncodeError' when using Cyrillic symbols to override field upload_to.

Django
#30811: Django throws 'UnicodeEncodeError' when using Cyrillic symbols to override
field upload_to.
-------------------------------------+-------------------------------------
     Reporter:  persik-dev           |                    Owner:  persik-
                                     |  dev
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  2.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  UnicodeEncodeError,  |             Triage Stage:
  Cyrillic, upload_to, File,         |  Unreviewed
  FileField                          |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by persik-dev):

 * type:  Uncategorized => Bug


--
Ticket URL: <https://code.djangoproject.com/ticket/30811#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/068.2b081ad20ab3149b98baed726247caa4%40djangoproject.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #30811: Django throws 'UnicodeEncodeError' when using Cyrillic symbols to override field upload_to.

Django
In reply to this post by Django
#30811: Django throws 'UnicodeEncodeError' when using Cyrillic symbols to override
field upload_to.
-------------------------------------+-------------------------------------
     Reporter:  persik-dev           |                    Owner:  persik-
                                     |  dev
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  2.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  UnicodeEncodeError,  |             Triage Stage:
  Cyrillic, upload_to, File,         |  Unreviewed
  FileField                          |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by persik-dev:

Old description:

> So, I have a ''Book'' model, that contains some fields. But most
> important are:
> **'title' as 'CharField'**
> **'pdf', 'epub', 'fb2' as 'FileField'**
> **'image' as 'ImageField'**
> we will return to they in just a second...
> My ''Book'' model also has overrided ''clean'' method. In that method I
> assign my ''title'' field value as ''upload_to'' value.
>
> {{{
> uploading_files = [
>     self.image,
>     self.pdf,
>     self.fb2,
>     self.epub
> ]
> for file_ in uploading_files:
>     file_.field.upload_to = self.title
> }}}
> Here is where bug hided. If I set ''title'' value to "Колобок", for
> example, it throws me an ''UnicodeEncodeError''. This thing happends only
> when i first create model.
> If I firstly will set ''title'' value as "Kolobok", and later rename it
> to "Колобок", I have no error.
>
> ----
>
> I already some kind of "fixed" this.
> Endpoint of thrown exception was
> **...\django\db\models\fields\files.py**. After fork from your Github, I
> followed this path, and find function that throws that error, that
> function was ''generate_filename'' in ''FileField'' class, line 305.
> After I added small correct...
>
> {{{
> try:
>     dirname = datetime.datetime.now().strftime(self.upload_to)
> except UnicodeEncodeError:
>     dirname = self.upload_to
> }}}
>
> ... all works fine now, and I can use Cyrillic symbols as my ''title''
> and folder name in ''upload_to'' attribute.
New description:

 So, I have a ''Book'' model, that contains some fields. But most important
 are:
 **'title' as 'CharField'**
 **'pdf', 'epub', 'fb2' as 'FileField'**
 **'image' as 'ImageField'**
 we will return to they in just a second...
 My ''Book'' model also has overrided ''clean'' method. In that method I
 assign my ''title'' field value as ''upload_to'' value.

 {{{
 uploading_files = [
     self.image,
     self.pdf,
     self.fb2,
     self.epub
 ]
 for file_ in uploading_files:
     file_.field.upload_to = self.title
 }}}
 Here is where bug hided. If I set ''title'' value to "Колобок", for
 example, it throws me an ''UnicodeEncodeError''. This thing happends only
 when i first create model.
 If I firstly will set ''title'' value as "Kolobok", and later rename it to
 "Колобок", I have no error.

 ----

 I already some kind of "fixed" this.
 Endpoint of thrown exception was **...\django\db\models\fields\files.py**.
 After fork from your Github, I followed this path, and find function that
 throws that error, that function was ''generate_filename'' in
 ''FileField'' class, line 305. After I added small correct...

 {{{
 try:
     dirname = datetime.datetime.now().strftime(self.upload_to)
 except UnicodeEncodeError:
     dirname = self.upload_to
 }}}

 ... all works fine now, and I can use Cyrillic symbols as my ''title'' and
 folder name in ''upload_to'' attribute.

 ----
 For comfort, here is my [https://github.com/persik-dev/LibraLive Github]
 with all ''my'' Django code in it.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30811#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/068.2a73ba75448d45248d503cd28bf4ace2%40djangoproject.com.
Reply | Threaded
Open this post in threaded view
|

Re: [Django] #30811: Django throws 'UnicodeEncodeError' when using Cyrillic symbols to override field upload_to.

Django
In reply to this post by Django
#30811: Django throws 'UnicodeEncodeError' when using Cyrillic symbols to override
field upload_to.
-------------------------------------+-------------------------------------
     Reporter:  Kirill               |                    Owner:  Kirill
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  UnicodeEncodeError,  |             Triage Stage:
  Cyrillic, upload_to, File,         |  Unreviewed
  FileField                          |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by felixxm):

 * status:  assigned => closed
 * version:  2.2 => master
 * resolution:   => invalid


Comment:

 Thanks for this report, however I don't think that it is an issue in
 Django, but rather in your implementation if you want to set `upload_to`
 dynamically you should set it to a callable, e.g.
 {{{

 def callable_upload_to(instance, filename):
     return posixpath.join(instance.title, filename)

 class Book(models.Model):
     pdf = FileField(..., upload_to=callable_upload_to)
     ...
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30811#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

--
You received this message because you are subscribed to the Google Groups "Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/068.fda568b8cf587b4e30c5bd8caafda851%40djangoproject.com.