status quo:
xadmin details page is the default display a row of field
Use form_layout:
1. You can set the required and optional fields
2. The field is not displayed may be provided,
3. You can not edit the fields set
First, the unused form_layout before
models.py
class ArticleClassify(models.Model): '''文章分类''' article_classify = models.CharField(max_length=30, verbose_name="分类", default="") def __str__(self): return self.article_classify class Meta: verbose_name = "文章分类" verbose_name_plural = verbose_name class ArticleDetail(models.Model): '''文章''' title = models.CharField(max_length=30, verbose_name="Title " , default = " Enter your caption " ) # title the Classify = models.ForeignKey (ArticleClassify, on_delete = models.CASCADE, the related_name = " classify_name " , verbose_name = " article categories " , ) body = models.TextField (verbose_name = " body " , default = " input text " ) the authModels.CharField = (10 = max_length, verbose_name = " author " , default = " ADMIN " ) the Detail = models.TextField (verbose_name = " Remarks " , default = " Add Note " ) # Create a time create_time = models.DateTimeField (auto_now_add True =, verbose_name = " created " ) # last updated update_time = models.DateTimeField (True auto_now =, = verbose_name " last updated " ) DEF __str__ (Self): return self.title class Meta: verbose_name = " article list " verbose_name_plural = ' list of articles '
adminx.py registration
class ControlArc(object): list_display = ['title','body','auth'] class ControlArticleClassify(object): list_display = ['article_classify'] xadmin.site.register(ArticleDetail,ControlArc)
Perform database table to generate and synchronize
python3 manage.py makemigrations
python3 manage.py migrate
Browser Display:
Second, unused form_layout after
Use form_layout Relayout, modify the contents adminx.py start xadmin.layout need to use imported classes: from xadmin.layout Import the Main, TabHolder, the Tab, The fieldset, Row, CoI, AppendedText, Side, Field, each attribute explanation: 1. form_layout tuple parameters which pass 2. Fieldse is provided a block title name, the default is the first parameter 3. row line display contents are provided, a plurality of parameters can be displayed in a row
4. the optional field is provided in the models field set which, when combined with parameters: blank = True. May simultaneously add blank = True, null = True
5. The read only field may be provided, add = readonly_fields [ 'AAA']
6. The display is not a field, a = the exclude [ 'the auth']
7. The above a few blocks, hold down the mouse can drag up and down position, if not to drag can add a parameter: unsort is not allowed to drag no_title name is not displayed title block
models.py
from xadmin.layout import Main, TabHolder, Tab, Fieldset, Row, Col, AppendedText, Side, Field
class MoreArc (Object): list_display = [ ' title ' , ' body ' , ' the auth ' ] readonly_fields = [ ' Detail ' ] # readonly form_layout = ( The fieldset (U ' title bar ' , Row ( ' title ' , ' the auth ' ), # row represents the inside of the display field as a row row ( ' Classify '), Css_class = ' Unsort ' # prevent drag block ), The fieldset ( ' text content ' , # The fieldset first parameter section name ' body ' , css_class = ' Unsort ' # prevent drag block ) , The fieldset (( ' Remarks ' ), Row ( ' Detail ' ), css_class = ' Unsort no_title ' # No_title title name is not displayed block ), ) # does not show a field the exclude = [ ' the auth ' ] xadmin.site.register (ArticleDetail, MoreArc)
Browser Display: