Reputation: 131
I am trying to figure out what class Meta:
really do in Django.
I come across with the code below in DRF, but not sure why under class Meta:
there is model = User
and fields = [...]
. Does it help to create a database?
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(
serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'groups']
And also what is the different with the class Meta:
used in Django as below.
from django.db import models
class Ox(models.Model):
horn_length = models.IntegerField()
class Meta:
ordering = ["horn_length"]
verbose_name_plural = "oxen"
I have tried to get further understanding from both Django and DRF documentation however I did not see the explanation for model = ...
and fields = [...]
used in DRF class Meta
.
Upvotes: 2
Views: 1502
Reputation: 1056
The concept of Meta class comes from metaprogramming. The term metaprogramming
refers to manipulating itself. Python supports a form of metaprogramming for classes called metaclasses. Meta class is mainly the inner class of your main class. Meta class is basically used to change the behavior of your main class attributes. It’s completely optional to add a Meta class to your Class. But in your Django project, you have already seen this metaclass concept available in different places like models.py, serializers.py, admin.py, etc.
Actually, this Meta class changes the common behavior of its main class like the model metaclass changing behavior using verbose_name
, db_table
, proxy
, permissions
, ordering
etc, and a lot of other options. Meta class in serializer also does the exact same things it tells it the model to use and what fields to serialize by using fields
, exclude
, and model
.
Upvotes: 2
Reputation: 1122022
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.
Upvotes: 6
Reputation: 342
class Meta is used in DRF serializers to configure your serializer.
model
defines the model to which your serializer is linked.
fields
is a list of properties that you would like to serve in your API.
Use fields = ['__all__']
to serve all properties
Use exclude = ['your_excluded_prop_1', 'your_excluded_prop_2']
to exclude properties
Upvotes: 4