class SmartPhoneModelSerializer(TranslatableModelSerializerMixin, ProductSerializer): multilingual = TranslatedFieldsField() operating_system = ValueRelatedField(model=OperatingSystem) variants = SmartphoneVariantSerializer(many=True) class Meta(ProductSerializer.Meta): model = SmartPhoneModel def create(self, validated_data): variants = validated_data.pop('variants') product = super().create(validated_data) for variant in variants: SmartPhoneVariant.objects.create(product=product, **variant) return product
class ProductSerializer(serializers.ModelSerializer): product_model = serializers.CharField(read_only=True) manufacturer = ValueRelatedField(model=Manufacturer) caption = TranslatedField() cms_pages = CMSPagesField() images = ImagesField() class Meta: exclude = ['id', 'polymorphic_ctype', 'updated_at'] def create(self, validated_data): cms_pages = validated_data.pop('cms_pages') images = validated_data.pop('images') product = super(ProductSerializer, self).create(validated_data) for page in cms_pages: ProductPage.objects.create(product=product, page=page) for image in images: ProductImage.objects.create(product=product, image=image) return product
from shop.serializers.catalog import CMSPagesField, ImagesField, ValueRelatedField {%- if cookiecutter.products_model == 'polymorphic' %} from {{ cookiecutter.app_name }}.models import (Commodity, SmartCard, SmartPhoneModel, SmartPhoneVariant, Manufacturer, OperatingSystem, ProductPage, ProductImage) {%- elif cookiecutter.products_model == 'smartcard' %} from {{ cookiecutter.app_name }}.models import SmartCard, Manufacturer, ProductPage, ProductImage {%- elif cookiecutter.products_model == 'commodity' %} from {{ cookiecutter.app_name }}.models import Commodity, ProductPage, ProductImage {%- endif %} from .translation import TranslatedFieldsField, TranslatedField, TranslatableModelSerializerMixin class ProductSerializer(serializers.ModelSerializer): product_model = serializers.CharField(read_only=True) {%- if cookiecutter.products_model != 'commodity' %} manufacturer = ValueRelatedField(model=Manufacturer) images = ImagesField() {%- endif %} caption = TranslatedField() cms_pages = CMSPagesField() class Meta: exclude = ['id', 'polymorphic_ctype', 'updated_at'] def create(self, validated_data): cms_pages = validated_data.pop('cms_pages') {%- if cookiecutter.products_model != 'commodity' %} images = validated_data.pop('images') {%- endif %} product = super(ProductSerializer, self).create(validated_data) for page in cms_pages: