1

我想在 django admin 中插入后创建一个像记录 ID 这样的目录,我该如何以及在哪里可以做到这一点。

我在 save_model 中尝试,但它不起作用,Gallery.ID 返回无

模型:

class Gallery(models.Model):
    title = models.CharField(max_length=250)
    added = models.DateTimeField()
    active = models.BooleanField()

    def __unicode__(self):
        return self.title

和 AdminModel

class GalleryAdmin(admin.ModelAdmin):
    save_on_top = True
    list_filter = ('active',)
    list_display = ('title','active','added')
    ordering = ['-added']
    inlines = [ImagesInline]

    def save_model(self, request, Gallery, form, change):
        if not change:
            Gallery.title = str(Gallery.id)
        Gallery.save()
4

2 回答 2

1

将您更改GalleryAdmin为此...您需要save()Gallery实例获取其id.

def save_model(self, request, Gallery, form, change):
    Gallery.save()
    if not change:
        Gallery.title = str(Gallery.id)
于 2012-05-13T09:50:34.220 回答
1
def save_model(self, request, Gallery, form, change):
    if Gallery.id is None:
        Gallery.save()
    if not change:
        Gallery.title = str(Gallery.id)
    Gallery.save()

save()这只是第一次做两个。

于 2012-05-13T10:25:35.437 回答