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.
* 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>