Opened 6 weeks ago

Closed 6 weeks ago

#35814 closed Uncategorized (invalid)

Django migrations & default JSONField

Reported by: FormindMPO Owned by:
Component: Migrations Version: 4.2
Severity: Normal Keywords: JSONField, default
Cc: FormindMPO Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (1)

comment:1 by Simon Charette, 6 weeks ago

Resolution: invalid
Status: newclosed

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 this link.

TicketClosingReasons/UseSupportChannels

Note: See TracTickets for help on using tickets.
Back to Top