类CardSerializer(serializers.ModelSerializer):
image = serializers.CharField(read_only=True)
class Meta:
model = Card
fields = ('id', 'template', 'data', 'MD5', 'image_md5',
'image', 'target_url', 'title', 'description',
'created', 'updated', 'createdBy')
read_only_fields = ['MD5', 'image_md5', 'image', 'createdBy']
def create(self, validated_data):
user = self.context.get('request').user
if not user.id:
raise ValidationError("Login Required")
validated_data['createdBy'] = user
return super(CardSerializer, self).create(validated_data)
def update(self, instance, validated_data):
new_ins = Card(**validated_data)
md5 = new_ins.gen_md5()
lookup = Card.objects.filter(MD5=md5).first()
if lookup:
return lookup
return super(CardSerializer, self).update(instance, validated_data)