1

我希望能够在 django admin 中一次添加多个记录。

模型.py

class Photo(models.Model):
    pub_date = models.DateTimeField('date published')
    sort = models.IntegerField()
    photo_category = models.ForeignKey(Photocategory)
    title = models.CharField(max_length=200)
    title_url = models.SlugField(max_length=200)
    main_image = models.ImageField(blank=True, null=True, upload_to='images/photo/')
    # size is "width x height"
    cropping = ImageRatioField('main_image', '350x350')

    def image_thumbnail(self):
       return '<a href="/media/%s" target="_blank"><img width="160px" src="/media/%s"/></a>' % (self.main_image, self.main_image)

    image_thumbnail.allow_tags = True
    image_thumbnail.short_description = 'main_image'

    def __unicode__(self):
        return self.title

管理员.py

class PhotoAdmin(ImageCroppingMixin, admin.ModelAdmin):

    prepopulated_fields = {'title_url': ('title',)}
    list_display = ('title', 'photo_category', 'image_thumbnail', 'sort')
    list_filter = ['photo_category']
    admin.site.register(Photo, PhotoAdmin)

截屏: django 管理面板

有没有一种方法可以让我在 1 个屏幕上一次说 5 个,1 比 1 填充它非常慢。我可以在 mysql 中使用 SQL 查询来做到这一点,但我想在这里实现这一点。

谢谢

4

1 回答 1

0

Hey Yes this is possible but Out of the Box,,

Here is what you will have to do:

  1. Define a Formset (to create multiple photo forms) in form Attribute of PhotoAdmin

  2. You will see a formset on the add photo Admin page, and should work correctly. If you don't then you can over-ride the templates by providing add_form_template and change_form_template attributes in Admin and display that form-set templates.

  3. Handle Saving of Multiple objects in the Formset

For example:

# admin.py

@admin.register(Photo)
class PhotoAdmin(ImageCroppingMixin, admin.ModelAdmin):
    prepopulated_fields = {'title_url': ('title',)}
    list_display = ('title', 'photo_category', 'image_thumbnail', 'sort')
    list_filter = ['photo_category']
        
    form = MyFormset
    add_form_template = "photo/admin/my_add_form.html"
    change_form_template = "photo/admin/my_change_form.html"

This is a rough workflow, you will have to try this and make modification if required. You can refer to this documentation: https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#modeladmin-options

于 2014-05-01T06:10:38.493 回答