我想在django admin中插入后,创建一个像记录ID的目录,我该在哪里以及该怎么做。

我尝试将其保存到save_model,但是它不起作用,Gallery.ID返回None

模型:

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()

最佳答案

将您的GalleryAdmin更改为此...。save()实例获取其Gallery之前,需要先id

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

关于python - 如何在django admn中插入后获取记录ID?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10570647/

10-12 21:45