old_contents: from rest_framework import serializers
  
  from apps.plea.models import Case, UsageStats, Offence
  
  
  class OffenceSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = Offence
          exclude = ("case",)
  
  
  class CaseSerializer(serializers.ModelSerializer):
      urn = serializers.RegexField("^\d{2}/[a-zA-Z]{2}/\d+/\d{2}$", max_length=16, min_length=14)\
  
      offences = OffenceSerializer(many=True)
  
      class Meta:
          model = Case
          fields = ("offences", "urn", "title", "name", "forenames", "surname",
                    "case_number")
  
      def create(self, validated_data):
  
          # Create the case instance
          offences = validated_data.pop("offences", [])
  
          case = Case.objects.create(**validated_data)
  
          # Create or update each page instance
          for item in offences:
              offence = Offence(**item)
              offence.case = case
              offence.save()
  
          return case
  
      def validate(self, attrs):
  
          if not Case.objects.can_use_urn(attrs["urn"]):
  
              raise serializers.ValidationError("Case data already exists")
  
          return attrs
  
  
  class UsageStatsSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = UsageStats
new_contents: from rest_framework import serializers
  
  from apps.plea.models import Case, UsageStats, Offence
  
  
  class OffenceSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = Offence
          exclude = ("case",)
  
  
  class CaseSerializer(serializers.ModelSerializer):
      case_number = serializers.CharField(required=True)
  
      offences = OffenceSerializer(many=True)
  
      class Meta:
          model = Case
          fields = ("offences", "urn", "title", "name", "forenames", "surname",
                    "case_number")
  
      def create(self, validated_data):
  
          # Create the case instance
          offences = validated_data.pop("offences", [])
  
          case = Case.objects.create(**validated_data)
  
          # Create or update each page instance
          for item in offences:
              offence = Offence(**item)
              offence.case = case
              offence.save()
  
          return case
  
      def validate_case_number(self, value, key):
          """
          Make sure case number is unique
          """
          #import pdb; pdb.set_trace()
          try:
              Case.objects.get(case_number=value[key])
          except (Case.DoesNotExist, Case.MultipleObjectsReturned ):
              return value
          else:
              raise serializers.ValidationError("Case with this case number already exists")
  
  
  class UsageStatsSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = UsageStats
subject: make api require unique case number, not urn