Skip to main content

gm2m_relations

The rest-framework-gm2m-relations library provides read/write serialization for django-gm2m field.

Requirements

Any currently-supported combination of rest-framework-generic-relations, Django REST Framework, Python, and Django.

Installation

Install using pip :

pip install gm2m-relations

API Reference

GM2MSerializer

serializes django generic many-to-many field. For a primer on generic many-to-many field, first see: https://github.com/tkhyn/django-gm2m

Let's assume a Post model which has a generic m2m relationship with other arbitrary models:

from gm2m import GM2MField


class Post(models.Model):
    text = models.SlugField()

    author = GM2MField()

And the following two models, which may have associated authors:

class User(models.Model):
    name = models.CharField()

class Organ(models.Model):
    name = models.CharField()

Now we define serializers for each model that may get associated with posts.

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('name',)

class OragnSerializer(serializers.ModelSerializer):
    class Meta:
        model = Organ
        fields = ('name',)

The model serializer for the Post model could look like this:

from gm2m_relations.serializers import GM2MSerializer

class PostSerializer(serializers.ModelSerializer):
    """
    A `Post` serializer with a `GM2MField` mapping all possible
    models to their respective serializers.
    """
    author = GM2MSerializer(
        {
            User: UserSerializer(),
            Organ: OragnSerializer(),
        },
        many=True,
    )

    class Meta:
        model = Post
        fields = ('text', 'author')

The JSON representation of a Post object with text='django' and its generic m2m field pointing to User object with name='Ali' would look like this:

{
    "author": [
        {
            "name" : "Ali",
        }
    ],
    "text": "django"
}

Writing to generic many to many field

The above PostSerializer is also writable. By default, a GenericRelatedField iterates over its nested serializers and returns the value of the first serializer that is actually able to perform to_internal_value() without any errors.

post_serializer = PostSerializer(
    data={
        'text': 'python',
        'author': [
            {
                "name" : "Ali",
            },
        ],
    },
)

post_serializer.is_valid()
post_serializer.save()

If you feel that this default behavior doesn't suit your needs, implement your own way of decision-making.

Metadata

Release files for gm2m-relations 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gm2m-relations 1.0.3
File Size Uploaded Provenance Actions
gm2m_relations-1.0.3.tar.gz 3.7 kB Without provenance Details for gm2m_relations-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for gm2m-relations 1.0.3
File Interpreter ABI Platform Actions
gm2m_relations-1.0.3-py3-none-any.whl Python 3 none any Details for gm2m_relations-1.0.3-py3-none-any.whl

Total release size: 7.9 kB

Release files / gm2m_relations-1.0.3.tar.gz

Download URL gm2m_relations-1.0.3.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ec3afe8bfccf362b22e9a451b36b5878a781f0484999c20c139289b7a2b4448f
BLAKE2b-256 checksum
How to use checksums
700260f0389f3014e57e4e33383576c29bc66328697040f767b5bb5d7c00f887
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / gm2m_relations-1.0.3-py3-none-any.whl

Download URL gm2m_relations-1.0.3-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ff5f81b412b947992fe968421c8310e632993dcf4fa2edaba2fae5ce85de2558
BLAKE2b-256 checksum
How to use checksums
9d5a7a1d2cfa06c590a8d7d11de25cdfdf4d4f82a6f7b85410cd931bba3f21dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Security for gm2m-relations 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page