Restframework Framework writes the personal understanding and problems encountered in the API

Source: Internet
Author: User

1. Explicitly deal with the object, in the process of restframework, if it is for the model write view, Queryset is to be shown the set of objects to be displayed, Serializer_class is the processing of each object to be used.
class Guidewordviewset (mixins. Listmodelmixin, Viewsets. Genericviewset):    "" " article     index" "" = GuideWord.objects.all ()    = Guidewordserializer
2. Deserialization, I do the deserialization is used for foreign keys, when a model has a foreign key, set the Related_name, and then in the process of serialization can be the same foreign key object object in the same foreign key object in the sequence data display, However, the name of the field should be related_name, and it is important to note that some unnecessary fields in the serialization process can be ignored.
Company = models. ForeignKey (company, Related_name='Company_video', verbose_name='company', Blank=true, null=True) (Foreign key field in model)classGuidevideoserializer (serializers. Modelserializer):classMeta:model=guidevideo Fields= ('ID','title','Video','Country',)classRecompanyserializer (serializers. Modelserializer): Company_video= Guidevideoserializer (Many=true, read_only=True)classMeta:model= Company Fields= ('ID','name','Country','Image','Company_video',)
3.model level, you can save data by rewriting the Save method to the database after processing, I encountered the situation is to use djangoueditor upload pictures and video when the data saved to the database contains double quotation marks, the result will be serialized when the escape character is added, This makes it inconvenient to read resources on the front-end, so I rewrote the model's Save method, replacing the double quotes in the field data with single quotes before the fields are stored in the database, so that the escape character backslash is not forced to be added during subsequent serialization.
    def Save (self, *args, * *Kwargs)        := self.content.replace ('"')  '")        return super (Guideword, self). Save (*args, **kwargs)
4. Another is the Djangoueditor upload path problem, when the integration of Ueditor in Xadmin, as long as the adminx.py in the definition of a class Modeladmin classes, in which style_fields = {' Field ': ' Ueditor '} and then adding the Modeladmin class to the corresponding registration object, you can use the Rich Text editor when you edit the field in xadmin background. To get back to the point, Djangoueditor also needs to define the required field in model as Ueditorfield, which inherits from TextField, which has the upload path configuration for pictures and attachments, but does not give the upload path configuration of the video directly, Then look at the GitHub editor's introduction to see the upload video path configuration, only need to set the upload_settings to:
upload_settings={'imagemaxsize': 1204000,                            'videopathformat  '"guide/videos/% (basename) s_% (datetime) s.% ( Extname) s"}

About Djangoueditor in the previous article also introduced, there is a need to see a ha ...

Restframework Framework writes the personal understanding and problems encountered in the API

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.