Opened 2 years ago

Closed 2 years ago

#33763 closed Uncategorized (invalid)

Django admin TabularInline in a Multiple databases context

Reported by: moccand Owned by: nobody
Component: contrib.admin Version: 3.2
Severity: Normal Keywords: admin.TabularInline admin multiple database
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

All my models are stored in a dedicated database. Django admin is used as "backoffice".

The id fields are set in all my models, and Class meta define for each model the targeted table.

    class MyModel(models.Model):
        id = models.IntegerField(primary_key=True)
        ...
        class Meta:
            managed = False
            db_table = '__mymodel_table'
            app_label = 'catalog'

So for my models i override the save methode in order to use the sequences and autoincrement already in the external database :

def save(self, *args, **kwargs):
    if not self.id :
        self.id = getNextId("__mymodel_id_seq")
    super(MyModel, self).save(*args, **kwargs)

the getNetId function is :

def getNextId(sequence):
    cursor = connections['catalogu'].cursor()
    try :
        with cursor as cursor:
            cursor.execute("select nextval('{}'::regclass)".format(sequence))
            result = cursor.fetchall()
            return result[0][0]
    except :
        return None

In addition all my modelAdmin (except the TabularInline) inherite from :

class MultiDBModelAdmin(admin.ModelAdmin):
    # A handy constant for the name of the alternate database.
    using = 'catalog'

My TabularInLine :

class MyModelAdminInline(admin.TabularInline):
    model = MyModel
    fields = ['id', 'titre_classe', 'valeur_texte', 'valeur_min', 'valeur_max', 'id_style', 'ordre', 'rayon', 'style_css',]

and it is used in an other Admin :

MySecondModelAdmin.inlines = [MyModelAdminInline,]

With the TabularInline models it seems i must set the id fields manually.

If let it blank : i get a validation error and the field id is shown in red
If set it readonly : i get the same error
If not shown in the inline : error too ...
But setting manualy a good value for the id it works fine.

I think all those 4 cases should work the same : the model save() method should be applied

Change History (1)

comment:1 by Mariusz Felisiak, 2 years ago

Resolution: invalid
Status: newclosed

Thanks for the report, however manipulating a primary key dynamically is complicated. You should set it in advance it you want to display it in a creation form.

Please use one of support channels if you're having further question or trouble understanding how Django works.

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