The Meta
class is merely a convenient place to group metadata (meaning data about the data) that DRF needs to adjust its configuration, but keep this separate from the attributes of the class itself. This separation allows the Django Rest Framework (and the wider Django Framework ecosystem) to avoid clashes between configuration and the actual class definitions.
The use of an inner Meta
class is a common pattern throughout Django, because this allows you to both keep this configuration separate from the fields of the class and keep it connected to the class in a way that's easy to read and easy for the framework to find. A DRF selialiser class should normally have one or more fields to help turn data into a serialized form, but the HyperlinkedModelSerializer
base class can generate these fields for you if you tell it what model you wanted to serialise. The fields on the Meta
class tell it you want to serialize specific fields from your User
model.
By putting this configuration on the inner Meta
class, they are kept in a separate namespace from the main class, but at the same time remain connected to the class they are meant to configure. Imagine a model that has a field named model
and fields
for example. If the HyperlinkedModelSerializer
required that configuration is found in the subclass itself, you could never produce a serializer that could process something with model
and fields
fields!
If you wanted to know what the different options are you can use on the inner Meta
class, you need to read the ModelSerialiser
and HyperlinkedModelSerializer
documentation in the API guide section.
For Django Models, you can refer to the Model Meta
chapter of the Django documentation. As I stated above, it's the same concept but here the Meta
class configures the database fields that the model supports and how the model relates to other database models you may have defined.
Last but not least, there is another answer here that confuses the term Meta with Python Metaclasses, which is a very different concept. While DRF and Django model classes lean heavily on metaclasses for their internal implementation, the class Meta:
definition you use to configure the framework functionality, they are not metaclasses. They are plain classes that are only used because they make for a convenient namespace.