コード例 #1
0
class StockSerializer(serializers.ModelSerializer):

    # user = serializers.HiddenField(default=CurrentUserDefault())
    organization = serializers.HiddenField(default=CurrentCompanyDefault())
    consumable_vo = ConsumableSerializer(source='consumable', read_only=True)
    storagesite_vo = StorageSitesSerializer(source='storagesite',
                                            read_only=True)
    supplier_vo = SupplierSerializer(source='supplier',
                                     read_only=True,
                                     allow_null=True)
    consumable = ConsumableSerializer(write_only=True)
    storagesite = serializers.PrimaryKeyRelatedField(
        queryset=StorageSites.objects.all(), write_only=True)
    supplier = serializers.PrimaryKeyRelatedField(
        queryset=Supplier.objects.all(), required=False, write_only=True)

    class Meta:
        model = Stock
        fields = Stock.common_fields + (
            'organization',
            'consumable_vo',
            'storagesite_vo',
            'consumable',
            'storagesite',
            'supplier_vo',
            'supplier',
        )

    def create(self, validated_data):
        print()
        consumable_data = validated_data.pop('consumable')
        consumable = Consumable.objects.create(**consumable_data)
        consumable.save()
        stock = Stock.objects.create(consumable=consumable, **validated_data)
        return stock
コード例 #2
0
class ClassificationSerializer(serializers.ModelSerializer):

    organization = serializers.HiddenField(default=CurrentCompanyDefault())

    class Meta:
        model = Classification
        fields = Classification.common_fields + ('organization', )
コード例 #3
0
class SupplierSerializer(serializers.ModelSerializer):

    organization = serializers.HiddenField(default=CurrentCompanyDefault())

    class Meta:
        model = Supplier
        fields = Supplier.common_fields + ('organization', )
コード例 #4
0
class UserSerizalizer(serializers.ModelSerializer):

    organization = serializers.HiddenField(default=CurrentCompanyDefault())
    role = serializers.IntegerField(default=1)
    password = serializers.CharField(default='123')

    class Meta:
        model = User
        fields = User.common_fields + (
            'role',
            'organization',
        )

    def validate(self, attrs):
        print(attrs['username'])
        print(attrs)
        # if not attrs['role']:
        #     raise BusinessValidationError(error_const.BUSINESS_ERROR.STAFF_NOT_EXIST)
        # print(attrs['role'])
        return attrs

    def create(self, validated_data):
        role_id = validated_data.pop('role')
        print(role_id)
        # role = Role.objects.get(role_id=role_id)
        user = User.objects.create(**validated_data)
        # user.role.add(role)
        # for index, role_data in enumerate(roles_data):
        #     role = Role.objects.filter(**role_data).first()
        #     print(role_data['role_id'])
        #     user.role.add(role)
        # user.save()
        return user
コード例 #5
0
class LaboratorySerializer(serializers.ModelSerializer):

    organization = serializers.HiddenField(default=CurrentCompanyDefault())

    class Meta:
        model = Lab
        fields = Lab.common_fields + ('organization', )
コード例 #6
0
class StorageSitesSerializer(serializers.ModelSerializer):

    organization = serializers.HiddenField(default=CurrentCompanyDefault())

    class Meta:
        model = StorageSites
        fields = StorageSites.common_fields + ('organization', )
コード例 #7
0
class ApproveSerializer(serializers.ModelSerializer):

    organization = serializers.HiddenField(default=CurrentCompanyDefault())
    user_vo = UserSerizalizer(read_only=True, source='user')
    user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(),
                                              write_only=True)

    class Meta:
        model = Approve
        fields = Approve.common_fields + ('user_vo', 'organization', 'user')
コード例 #8
0
class ConsumableSerializer(serializers.ModelSerializer):

    organization = serializers.HiddenField(default=CurrentCompanyDefault())
    classification_vo = ClassificationSerializer(source='classification',
                                                 read_only=True)
    classification = serializers.PrimaryKeyRelatedField(
        queryset=Classification.objects.all(), write_only=True)

    # number = serializers.IntegerField()
    # s = serializers.PrimaryKeyRelatedField(source='StorageSites', read_only=True)

    class Meta:
        model = Consumable
        fields = Consumable.common_fields + ('organization', 'classification',
                                             'classification_vo')

    def create(self, validated_data):
        number = validated_data.pop('number')
        c = Consumable.objects.create(**validated_data)
        return c