How to process back-end generator parameters with the front-end template language

Source: Internet
Author: User

The database model is as follows:

Class Server (models. Model):
"""
Server information
"""
# asset = models. Onetoonefield (' Asset ')


IDC = models. ForeignKey (Idc,null=true, Blank=true)
Cabinet_num = models. Charfield (' Cabinet number ', max_length=30, Null=true, Blank=true)
Cabinet_order = models. Charfield ("Serial number in cabinet", max_length=30, Null=true, Blank=true)

Business_unit = models. ForeignKey (Businessunit,null=true, Blank=true)

tags = models. Manytomanyfield (TAG)


server_status_choices = (
(1, ' shelves '),
(2, ' online '),
(3, ' Offline '),
(4, ' bottom shelf '),
)

server_status_id = models. Integerfield (choices=server_status_choices, default=1)

hostname = models. Charfield (max_length=128, Unique=true)
SN = models. Charfield (' SN ', max_length=128, Db_index=true)
Manufacturer = Models. Charfield (verbose_name= ' manufacturer ', max_length=64, Null=true, Blank=true)
Model = models. Charfield (' model ', max_length=64, Null=true, Blank=true)

MANAGE_IP = models. Genericipaddressfield (' Management IP ', null=true, Blank=true)

Os_platform = models. Charfield (' System ', max_length=16, Null=true, Blank=true)
Os_version = models. Charfield (' System version ', max_length=128, Null=true, Blank=true)

Cpu_count = models. Integerfield (' Number of CPUs ', Null=true, Blank=true)
Cpu_physical_count = models. Integerfield (' CPU physical number ', Null=true, Blank=true)
Cpu_model = models. Charfield (' CPU model ', max_length=128, Null=true, Blank=true)

Create_at = models. Datetimefield (Auto_now_add=true, Blank=true)

Latest_date = models. Datetimefield (Null=true,blank=true)


Class Meta:
verbose_name_plural = "Server table"

def __str__ (self):
Return Self.hostname

For the server_status_id field in the model can only be a number, then how to display the field corresponding to the Chinese information?

This can be written in the View function:

def test (Request):

Server_list=models. Server.objects.all ()

For Server_obj in Server_list:

Server_obj[choice_name] = Server_obj.get_server_status_id_display ()

return render (Request, ' xxxxx.html ', {' server_list ': ser_list})

At this point we call the Get_server_status_id_display () method to display the ID corresponding to the choice Chinese, and re-loop assigned to each object new fields to correspond to choice Chinese.

But what happens if we use JS's additions and deletions to the components? What we got at that time was not a single object, but a database query implemented through AJAX.

At this point we can use the new method, using the template language of the front end to loop our backend to send past generators.

def xxx (data_list):

For I in Data_list:

For j in Models. Server.server_status_choices:
      

def test (Request):

Data_list=models. Server.objects.values (' id ', ' server_status_id ')

return render (Request, ' xxxxx.html ', {' data_list ': xxx (data_list)})

How to process back-end generator parameters with the front-end template language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.