[Django] #33763: Django admin TabularInline in a Multiple databases context

34 views
Skip to first unread message

Django

unread,
Jun 2, 2022, 4:31:31 AM6/2/22
to django-...@googlegroups.com
#33763: Django admin TabularInline in a Multiple databases context
-------------------------------------+-------------------------------------
Reporter: moccand | Owner: nobody
Type: | Status: new
Uncategorized |
Component: | Version: 3.2
contrib.admin | Keywords: admin.TabularInline
Severity: Normal | admin multiple database
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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

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

Django

unread,
Jun 2, 2022, 4:45:55 AM6/2/22
to django-...@googlegroups.com
#33763: Django admin TabularInline in a Multiple databases context
-------------------------------------+-------------------------------------
Reporter: moccand | Owner: nobody
Type: Uncategorized | Status: closed
Component: contrib.admin | Version: 3.2
Severity: Normal | Resolution: invalid
Keywords: admin.TabularInline | Triage Stage:
admin multiple database | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

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


Comment:

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
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels] if you're having further question or trouble
understanding how Django works.

--
Ticket URL: <https://code.djangoproject.com/ticket/33763#comment:1>

Reply all
Reply to author
Forward
0 new messages