在内联排序表中隐藏排序字段的grappelli(Django Admin)
从grappelli定制文档中,它建议:在内联排序表中隐藏排序字段的grappelli(Django Admin),django,django-models,django-admin,django-grappelli,Django,Django Models,Django Admin,Django Grappelli,从grappelli定制文档中,它建议: The sortable-field will not automatically be hidden (use a Hidden Input Widget if needed). 然而,我已经搜索了这么长时间,不知道什么是“隐藏的输入小部件”,也不知道如何实现Django模型。这是我的密码: # models.py class video(models.Model): category = models.ForeignKe
The sortable-field will not automatically be hidden
(use a Hidden Input Widget if needed).
然而,我已经搜索了这么长时间,不知道什么是“隐藏的输入小部件”,也不知道如何实现Django模型。这是我的密码:
# models.py
class video(models.Model):
category = models.ForeignKey(subCategory)
index = PositionField('index')
video_title = models.CharField(max_length=255, blank=True, null=True)
video_desc = models.TextField(blank=True, null=True)
main_img = S3EnabledImageField(upload_to='video_img', blank=True, null=True)
small_img = S3EnabledImageField(upload_to='video_img', blank=True, null=True)
mid_img = S3EnabledImageField(upload_to='video_img', blank=True, null=True)
large_img = S3EnabledImageField(upload_to='video_img', blank=True, null=True)
last_updated = models.DateField(auto_now=True)
date_added = models.DateField()
date_modified = models.DateField()
date_published = models.DateField(blank=True, null=True)
date_closed = models.DateField(blank=True, null=True)
status = models.CharField(max_length=7,choices=STATUS_CHOICE)
class Meta:
ordering = ('index',)
verbose_name = 'Video'
verbose_name_plural = 'Video'
def __unicode__(self):
return self.video_title
class video_file(models.Model):
video = models.ForeignKey(video)
index = models.PositiveIntegerField()
file_title = models.CharField(max_length=255, blank=True, null=True)
#main_file = models.ImageField(upload_to='phoneso/video_file', blank=True, null=True)
main_file = S3EnabledFileField(upload_to='video_file')
resolution = models.CharField(max_length=50, blank=True, null=True)
file_format = models.CharField(max_length=50, blank=True, null=True)
date_added = models.DateField(auto_now_add=True)
date_published = models.DateField(auto_now_add=True)
status = models.CharField(max_length=7,choices=STATUS_CHOICE)
class Meta:
ordering = ('index',)
verbose_name = 'Video File'
verbose_name_plural = 'Video File'
def __unicode__(self):
return self.video.video_title
# admin.py
class video_fileInline(admin.TabularInline):
fields = ('main_file' , 'resolution' , 'file_format' , 'status', 'index',)
sortable_field_name = 'index'
model = video_file
extra = 1
class videoAdmin(admin.ModelAdmin):
list_display = ('index' , 'video_title', 'category' , 'date_added' , 'date_published' , 'status')
search_fields = ['video_title', 'desc']
readonly_fields = ('date_added','date_modified')
list_filter = ['category']
inlines = [video_fileInline]
class video_fileAdmin(admin.ModelAdmin):
list_display = ('index' , '__unicode__' , 'file_title', 'resolution' , 'file_format' , 'main_file' , 'date_added' , 'date_published' , 'status')
search_fields = ['video_title', 'desc']
我应该在哪里实现建议的“隐藏输入小部件”
谢谢。您可以为您的模型编写表单,并在视频文件内联中使用它: forms.py
class VideoFileForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(VideoFileForm, self).__init__(*args, **kwargs)
# key should be your sortable-field - in your exaple it's *index*
self.fields['index'].widget = forms.HiddenInput()
class Meta:
model = video_file
管理员
class video_fileInline(admin.TabularInline):
fields = ('main_file' , 'resolution' , 'file_format' , 'status', 'index',)
form = VideoFileForm
sortable_field_name = 'index'
model = video_file
extra = 1
现在我们有了
葡萄素
您可以根据文档使用:
好主意,我对python/django不太熟悉,我不知道我们可以在默认的管理页面中重写表单对象。好的灵感!这个解决方案工作得很好,但是表单标签“index”仍然存在,有什么方法可以消除它吗?谢谢。另一方面,“super(VideoFileForm,self)。\uuuuu init\uuu(*args,**kwargs)”的含义是什么?要摆脱标签索引,请在模型中为索引字段放置一行:
self.fields['index'].label='
,或者在init方法的末尾放置verbose\u name='
。super()用于调用父类构造函数。谢谢,verbose_name=''可以工作,尽管表单元格仍然存在:dt要消除这种情况,可以使用CSS。例如,如果调用字段position
并使用tablarinline
,这将起作用:.grp-th.position{display:none!important}
。请注意,在声明内联类时,需要将基类按顺序排列(首先是grappelistortablehiddenmixin
,然后是admin.tablarinline
)。