Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement dynamic Multi-Marquee form by django-admin

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how django-admin realizes the relevant knowledge points of the dynamic multi-box form. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Background

With django-admin, you can quickly get the CRUD interface, but if you need to create a multi-select label field, you need to adjust the form

Example

Model.py

One tag (tag class), one book (book class)

Book carries tag, which does not use foreign keys with performance problems.

Class tag (models.Model): name= models.CharField (max_length=20, verbose_name=' tag') class book (models.Model): name= models.CharField (max_length=50, verbose_name=' title') tags = models.TextField (null=True, verbose_name=' tag')

Admin.py

Register the corresponding model in admin before it can be discovered and used by django-admin, so here are the main steps to note:

Values_list returns:

Keep the list of up-to-date tags: perform a tag query only once after the form initialization

Form modification display label: initializes the assignment list object in the form

# tag query def get_tag_list (): return tag.objects.values_list ("name", "name") # form class definition class bookForm (forms.ModelForm): tags = forms.MultipleChoiceField (label=' tag', widget=widgets.CheckboxSelectMultiple) def _ _ init__ (self, * args, * * kwargs): super (cameraForm, self). _ _ init__ (* args) * * kwargs) # ensure that each initialization has the latest tag list self.fields ['tags']. Choices = get_tag_list () # because it will be stored as a list string in the database You need to go back to the list object when assigning To recognize the original value self.initial ['tags'] = eval (self.initial [' tags']) class Meta: model = book exclude = [] # book extension class bookAdmin (admin.ModelAdmin): search_fields = 'name',' tags') list_display = ('name',' tags') form = bookForm # registered in admin (book) when modifying the form BookAdmin) these are all the contents of the article "how does django-admin implement a dynamic multi-box form" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report