Skip to main content

This library implements Django REST Framework serializers to handle generic many to many relations.

Project description

gm2m_relations

This library implements Django REST Framework serializers to handle generic many to many relations.

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gm2m_relations-1.0.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

gm2m_relations-1.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file gm2m_relations-1.0.0.tar.gz.

File metadata

  • Download URL: gm2m_relations-1.0.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for gm2m_relations-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6996838a8c29ee509281ad4b3d2e74d6bfb88b22dc4178afb41832750f9f82bb
MD5 9fa4deb40907b3c83e01bd242964b9df
BLAKE2b-256 03791ed747c9729794ffaa5162f94b7bf1f0a2338c09750a2b94edd19ef62e65

See more details on using hashes here.

File details

Details for the file gm2m_relations-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gm2m_relations-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f46788c667355d295c3f290f34b58a58e9cbb95adefe141d7b0a8989f79d4e7
MD5 b545370f7daf958a6c895eb65f783179
BLAKE2b-256 82ad59b3a514fa8cbc74c6067594bdab24e54dcaf88e42128415427c1fffbda3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page