The keyboard from the company arrived on Friday, after all, I would not buy such an expensive keyboard myself… However, the layout of this keyboard has changed considerably as the number of keys has shrunk to 60. To be familiar with clavier, arrange a few recent experience, in order to provide for reader.

Use Django REST Framework tipsCould not resolve URL for hyperlinked relationship using view name "user-detail"

In Django serializers, since I set the namespace name in urls.py in my app, Need in serializers. HyperlinkedIdentityField view_name specified name, such as:

class UserSerializer(serializers.HyperlinkedModelSerializer) :
    url = serializers.HyperlinkedIdentityField(view_name="myapp:user-detail")

    class Meta:
    model = User
    fields = ('url'.'username')
Copy the code

At this point, the problem is solved.

Django Models experience with Select fields

It’s actually pretty simple, and since my requirements only need to be displayed in the Django background, I just need to process them in the Models. Since I needed a lot of fields, I created a new file cons.py in the app directory to store fields.

Here is the model file models.py:

# -*- coding: UTF-8 -*-
from django.db import models
from .cons import SALCATEGORIES
from .cons import YEAR
from .cons import MONTH
from .cons import ORG

# Create your models here.
class Salary(models.Model) :
    month = models.CharField('month',max_length=50, choices=MONTH)
    year = models.CharField('years',max_length=10, choices=YEAR)
    org = models.CharField('Organization name',max_length=100, choices=ORG)
    create_timestamp = models.DateTimeField('Creation time',auto_now_add=True)
    change_timestamp = models.DateTimeField('Modification time',auto_now=True)
    def __str__(self) :
        return dict(YEAR)[self.year]+dict(MONTH)[self.month]+'_'+dict(ORG)[self.org]

    class Meta:
        db_table = 'salary'
Copy the code

The cons.py file introduces the following:

# -*- coding: UTF-8 -*-
SALCATEGORIES = (
    ('shubas'.'Payable - Basic salary'),
    ('shupos'.'Payable - Post wage'),
    ('shupls'.'Merit-based pay'),
    ('shuotr'.'Should be issued - other'),
    ('wihuem'.'Withholding - Unemployment Insurance'),
    ('wihmed'.'Withholding - Medical Insurance'),
    ('wihold'.'Withholding - Superannuation'),
    ('wihgjj'.'Withholding - Provident Fund'),
    ('wihbgj'.'Withholding - Supplementary Housing Provident Fund'),
    ('wihotr'.'Withholding - Other'),
    ('relsal'.'Take-home pay - Take-home pay'),
    ('relotr'.'Real - other'),
)

YEAR = (
    ('2020'.'2020'),
    ('2021'.'2021'),
    ('2022'.'2022'),
    ('2023'.'2023'),
    ('2024'.'2024'),
    ('2025'.'2025'),
    ('2026'.'2026'),
    ('2027'.'2027'),
    ('2028'.'2028'),
)

MONTH = (
    ('01'.'01 month'),
    ('02'.'present'),
    ('03'.'03 month'),
    ('04'.'04 month'),
    ('05'.'05月'),
    ('06'.'June'),
    ('07'.'July'),
    ('08'.'August'),
    ('09'.'09'),
    ('10'.'October'),
    ('11'.'November'),
    ('12'.'12 months'),
)

ORG = (
    ('abc'.ABC Company of China),
    ('cde'.Shanghai CDE Company),Copy the code

This key-value pair stores the previous key and displays the subsequent value

This form above is also an interesting thing about the type of the variable. As we all know, () is the initialization flag of a tuple. Normally, tuples cannot be converted directly to dictionaries, but variables like the above can be converted normally. See the following test code and output:

print(type(ORG))
for i in ORG:
    print(i[0] +"_"+i[1])
    print(type(i))

print(type(dict(ORG)))
print(dict(ORG))
for i in dict(ORG):
    print(i)
    print(type(i))
Copy the code

Output:

<class 'tuple'> CDE > Shanghai CDE <class 'tuple'> <class 'dict'> {' ABC ': 'ABC ',' CDE ': } ABC <class 'STR '> ABC <class' STR '>Copy the code

Back to the implementation. Finally, the background display I need is as follows:

The storage status of database tables is as follows:

Here’s a look at the choices Field for the Django model Field:

A sequence itself consists of the iterated terms of exactly two items (e.g. [(A, B), (A, B)… ) as an option for this field. If given choices, they will be enforced by model validation, and the default form widget will be a selection box with those choices rather than a standard text field.

The first element in each tuple is the actual value to be set on the model, and the second element is the human-readable name. Such as:

Copy the code

YEAR_IN_SCHOOL_CHOICES = [ (‘FR’, ‘Freshman’), (‘SO’, ‘Sophomore’), (‘JR’, ‘Junior’), (‘SR’, ‘Senior’), (‘GR’, ‘Graduate’), ]

> In general, it is best to define the selection inside the Model class and define a constant with an appropriate name for each value: > 'python > from Django.db import models > > class Student(models.model): FRESHMAN = 'FR' SOPHOMORE = 'SO' JUNIOR = 'JR' SENIOR = 'SR' GRADUATE = 'GR' YEAR_IN_SCHOOL_CHOICES = [ (FRESHMAN, 'Freshman'), (SOPHOMORE, 'Sophomore'), (JUNIOR, 'Junior'), (SENIOR, 'Senior'), (GRADUATE, 'Graduate'), ] year_in_school = models.CharField( max_length=2, choices=YEAR_IN_SCHOOL_CHOICES, default=FRESHMAN, ) > def is_upperclass(self): Return self.year_in_school in {self.junior, self.senior} > while you can define a list of choices outside the model class and then refer to it, defining choices and the name of each choice inside the model class, You can keep all this information in the classes that use it and help refer to those choices (for example, 'student.sophomore' will work wherever the 'Student' model is imported). You can also collect your available choices into named groups that can be used for organizational purposes:  > ``` MEDIA_CHOICES = [ ('Audio', ( ('vinyl', 'Vinyl'), ('cd', 'CD'), ) ), ('Video', ( ('vhs', 'VHS Tape'), ('dvd', 'DVD'), ) ), ('unknown', 'Unknown'), ]Copy the code

The first element in each tuple is the name applied to that group. The second element is an iteration of a tuple, each containing a value and a readable option name. Grouped options can be combined with ungrouped options in a single list (as in this case, the ‘unknown’ option).

For each model field that has choice set, Django adds a method to retrieve the readable name of the field’s current value. See get_FOO_display() in the database API documentation.

Note that the selection can be any sequence object — not necessarily a list or tuple. This allows you to construct choices dynamically. But if you find yourself making chips magic dynamic, you might be better off using a suitable database table with ForeignKey. Chips is for static data and should not change much, if at all.

A new migration is created every time the order of choices changes.

Unless Blank =False is set on the field with default, labels containing “———” will be rendered along with the selection box. To override this behavior, add a tuple containing None to Choices, For example (None, ‘Your String For Display’). Alternatively, you can replace None with an empty string where it makes sense — like in CharField.

Finally, the new equipment to the building. That’s the most expensive equipment on hand in years.