#35814: Django migrations & default JSONField
-------------------------------------+-------------------------------------
     Reporter:  FormindMPO           |                     Type:
                                     |  Uncategorized
       Status:  new                  |                Component:
                                     |  Migrations
      Version:  4.2                  |                 Severity:  Normal
     Keywords:  JSONField, default   |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 Hello,

 I'm working on a dynamic translated field (dynamic means i won't use i18n
 to make it)

 I want to use a JSONField in my Article model, as so :

 {{{
 title = models.JSONField(default=defaultTraduction)
 description = models.JSONField(default=defaultTraduction)
 }}}

 Because making usage of models.JSONField(default=dict(fr="",en="",es=""))
 raises a warning.

 My model is stored in a file named Article.py, inside a models folder.

 When loading my migration : python manage.py makemigrations, it tries to
 import the default function, as so :


 {{{
 migrations.AlterField(
             model_name="article",
             name="title ",
 field=models.JSONField(default=recon.models.article.defaultTraduction),
         ),
 }}}

 But this is not a function part of the class, it is part of the file, so i
 have the following error when migrating / changing migration :

 field=models.JSONField(default=recon.models.article.defaultTraduction),
 AttributeError: type object 'article' has no attribute 'defaultTraduction'

 :(

 I tried to override the value, with the actual value ->
 dict(fr="",en="",es=""), but it automaticly detects a modification on next
 migration
 I tried to move the function in another file -> same issue (type object
 '**my filename**' has no attribute 'defaultTraduction'
 I tried to move the function in another file, and put it in a class ->
 Then in my code it gives


 {{{
 class utils :
      def defaultTraduction():
          return dict(fr="",en="",es="")

 _____

 class Article
 title = models.JSONField(default=utils.defaultTraduction)
 }}}

 And now i have a recon.models.utils.utils.defaultTraduction (stacked
 utils)

 I seems that django doesn't really likes this defaut import, how to deal
 with ?
 Thank you
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35814>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107019257b73481-b7aaece7-37c8-49af-896e-75a6e9c23414-000000%40eu-central-1.amazonses.com.

Reply via email to