在Django Admin中,如果想要显示没有标签的ReadOnlyFields,可以通过自定义Admin类来实现。以下是实现的步骤: 1. 创建一个新的Admin类,继承自Django的admin...
所以在form.py文件中对在readonly_fields 列表里的字段添加一个 disabled 样式 for field_name,field_obj in cls.base_fields.items(): # 根据 cls.base_field 的特性给每个字段加上样式 field_obj.widget.attrs['class'] = 'form-control' if field_name in admin_class.readonly_fields: field_obj.widge...
class AnchorBindAgentAdmin(admin.ModelAdmin): def get_readonly_fields(self, request, obj=None): """ Hook for specifying custom readonly fie...
readonly_fields¶ 默认情况下,管理会将所有字段显示为可编辑。该选项中的任何字段(应该是 list 或tuple)将按原样显示其数据,不可编辑;它们也被排除在用于创建和编辑的 ModelForm 中。请注意,当指定 ModelAdmin.fields 或ModelAdmin.fieldsets 时,只读字段必须存在才能显示(否则将被忽略)。 如果没有通过 Model...
Django Readonly Field Make some Django model fields readonly. In other words, it lets you tell Django to read some fields from your database, but never try to write those back. It can be useful if your fields are populated by triggers or something. ...
teacher = serializers.ReadOnlyField(source='teacher.username') # 外键字段、只读 class Meta: model = Course # 写法和上面的CourseForm类似 # url是默认值,可在settings.py中设置URL_FIELD_NAME使全局生效 # fields = ('id', 'url', 'name', 'introduction', 'teacher', 'price', 'created_at', ...
9.如果readonly的字段比较多,还可以放在一起写: read_only_fields= ["x1", "x2", "x3"...] 例如:模型: class Publish(models.Model): name = models.CharField(verbose_name="出版社名称", max_length=64) email = models.EmailField(verbose_name="出版社邮箱", blank=True, null=True) def __...
不过,我不怎么用fields和exclude。用得比较多的是fieldsets。该设置可以对字段分块,看起来比较整洁。如下设置: fieldsets = ( ("base info", {'fields': ['caption', 'author', 'tags']}), ("Content", {'fields':['content', 'recommend']}) ...
This is used by AdminReadonlyField to render the field. Unfortunately, linebreaksbr replies on the template system autoescape mechanism, which isn't kicking in when calling the filter directly: In [2]: linebreaksbr('test') Out[2]: u'test' In [3]: type(_) Out[3]: django.utils.safe...
owner =serializers.ReadOnlyField(source='owner.username') liker_count = serializers.SerializerMethodField() is_like = serializers.SerializerMethodField() file_path = serializers.ImageField() class Meta: model = beauty_local exclude = ("liker",) ...