Skip to content

Commit

Permalink
🐛Make generic relation support optional
Browse files Browse the repository at this point in the history
- Fixes #322
  • Loading branch information
yezyilomo committed Dec 31, 2024
1 parent 03c17da commit 37304e2
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions django_restql/mixins.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from django.http import QueryDict
from django.db.models import Prefetch
from django.utils.functional import cached_property
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.fields.related import ManyToManyRel, ManyToOneRel
from django.contrib.contenttypes.fields import GenericRel
from django.contrib.contenttypes.models import ContentType
from django.http import QueryDict
from django.utils.functional import cached_property

try:
from django.contrib.contenttypes.fields import GenericRel
from django.contrib.contenttypes.models import ContentType
except ImportError:
GenericRel = None
ContentType = None

from rest_framework.serializers import ListSerializer, Serializer, ValidationError

Expand Down Expand Up @@ -637,8 +642,9 @@ def create_many_to_one_generic_related(self, instance, data):
field_pks = {}
nested_fields = self.restql_writable_nested_fields

content_type = ContentType.objects.get_for_model(instance)

content_type = (
ContentType.objects.get_for_model(instance) if ContentType else None
)
for field, values in data.items():
relation = getattr(self.Meta.model, field).field

Expand Down Expand Up @@ -735,12 +741,12 @@ def create(self, validated_data):
if isinstance(rel, ManyToOneRel):
value = validated_data_copy.pop(field)
fields["many_to"]["one_related"].update({field: value})
elif isinstance(rel, GenericRel):
value = validated_data_copy.pop(field)
fields["many_to"]["one_generic_related"].update({field: value})
elif isinstance(rel, ManyToManyRel):
value = validated_data_copy.pop(field)
fields["many_to"]["many_related"].update({field: value})
elif GenericRel and isinstance(rel, GenericRel):
value = validated_data_copy.pop(field)
fields["many_to"]["one_generic_related"].update({field: value})

foreignkey_related = {
**fields["foreignkey_related"]["replaceable"],
Expand All @@ -755,9 +761,11 @@ def create(self, validated_data):

self.create_many_to_one_related(instance, fields["many_to"]["one_related"])

self.create_many_to_one_generic_related(
instance, fields["many_to"]["one_generic_related"]
)
if fields["many_to"]["one_generic_related"]:
# Call create_many_to_one_generic_related only if we have generic relationship
self.create_many_to_one_generic_related(
instance, fields["many_to"]["one_generic_related"]
)

return instance

Expand Down Expand Up @@ -1050,12 +1058,12 @@ def update(self, instance, validated_data):
if isinstance(rel, ManyToOneRel):
value = validated_data_copy.pop(field)
fields["many_to"]["one_related"].update({field: value})
elif isinstance(rel, GenericRel):
value = validated_data_copy.pop(field)
fields["many_to"]["one_generic_related"][field] = value
elif isinstance(rel, ManyToManyRel):
value = validated_data_copy.pop(field)
fields["many_to"]["many_related"].update({field: value})
elif GenericRel and isinstance(rel, GenericRel):
value = validated_data_copy.pop(field)
fields["many_to"]["one_generic_related"].update({field: value})

instance = super().update(instance, validated_data_copy)

Expand All @@ -1071,7 +1079,9 @@ def update(self, instance, validated_data):

self.update_many_to_one_related(instance, fields["many_to"]["one_related"])

self.update_many_to_one_generic_related(
instance, fields["many_to"]["one_generic_related"]
)
if fields["many_to"]["one_generic_related"]:
# Call update_many_to_one_generic_related only if we have generic relationship
self.update_many_to_one_generic_related(
instance, fields["many_to"]["one_generic_related"]
)
return instance

0 comments on commit 37304e2

Please sign in to comment.