Metadata in Django

Source: Internet
Author: User
The meta class in Django is confused when it looks at the Form-related source code of Django (1.6), so django is excerpted. forms. forms. several code snippets in py are used to analyze how the metadata class is used in Django:

def with_metaclass(meta, *bases):    """Create a base class with a metaclass."""    return meta("NewBase", bases, {}) class DeclarativeFieldsMetaclass(type):    def __new__(cls, name, bases, attrs):        print('cls: %s, name: %s, bases: %s ,attrs: %s\n' % (cls, name, bases, attrs))         new_class = super(DeclarativeFieldsMetaclass, cls).__new__(cls, name, bases, attrs)#        new_class._meta = '123'        return new_class class BaseForm(object):    pass class Form(with_metaclass(DeclarativeFieldsMetaclass, BaseForm)):    pass     class MyForm(Form):    a = 1    b = 2

Load the Python module above, and the console will output:

Cls: , Name: NewBase, bases :( ,), Attrs :{}

Cls: , Name: Form, bases :( ,), Attrs: {'_ module _': '_ main __'}

Cls: , Name: MyForm, bases :( ,), Attrs: {'a': 1, '_ module _': '_ main _', 'B': 2}

Although metaclass is not directly specified for MyForm in code, because MyForm inherits from Form, and Form inherits from the class "NewBase" generated by DeclarativeFieldsMetaclass, DeclarativeFieldsMetaclass is actually the metaclass of MyForm.

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.