[Django] #35814: Django migrations & default JSONField

8 views
Skip to first unread message

Django

unread,
Oct 4, 2024, 9:28:02 AM10/4/24
to django-...@googlegroups.com
#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.

Django

unread,
Oct 4, 2024, 10:42:15 AM10/4/24
to django-...@googlegroups.com
#35814: Django migrations & default JSONField
------------------------------------+--------------------------------------
Reporter: FormindMPO | Owner: (none)
Type: Uncategorized | Status: closed
Component: Migrations | Version: 4.2
Severity: Normal | Resolution: invalid
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
------------------------------------+--------------------------------------
Changes (by Simon Charette):

* resolution: => invalid
* status: new => closed

Comment:

The goal of this issue tracker is to only track issues about Django
itself.

So, the best place to get answers to your issue is using any of the user
support channels from [https://docs.djangoproject.com/en/dev/faq/help
/#how-do-i-do-x-why-doesn-t-y-work-where-can-i-go-to-get-help this link].

TicketClosingReasons/UseSupportChannels
--
Ticket URL: <https://code.djangoproject.com/ticket/35814#comment:1>
Reply all
Reply to author
Forward
0 new messages