Django get_or_create()返回奇怪的行为

时间:2019-01-11 17:31:00

标签: python django

尝试通过<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id='select'> <option></option> <option value="Large Van - Sameday">Large Van - Sameday</option> <option value="Extra Large Van - Sameday">Extra Large Van - Sameday</option> </select>

获取或创建对象时遇到一个奇怪的错误
get_or_create()

build_case函数

ValueError: Cannot assign "(<CaseStatus: CaseStatus object (13)>,)": "PatientCase.status" must be a "CaseStatus" instance.

对象

def build_case(obj):

    data = {
        "case": {
            "case_id": obj.case_id,
            "uuid": obj.uuid.hex,
            "owner": {
                "uuid": obj.owner.userprofile.uuid.hex
            },
            "status": {
                "uuid": obj.status.uuid.hex,
                "message": obj.status.status,
                "extra_data": obj.status.extra_data,
            },
            "patient": {
                "uuid": obj.patient.uuid.hex,
                "details": {
                    "full_name": obj.patient.full_name,
                    "date_of_birth": obj.patient.date_of_birth.strftime(
                        "%Y-%m-%d"),
                    "practice": {
                        "uuid": obj.patient.practice.uuid.hex,
                        "name": obj.patient.practice.name
                    }
                }
            },
            "start_date": obj.start_date.strftime("%Y-%m-%d"),
            "case_photos": [
                photo.file_path for photo in obj.case_photos.all()],
            "forum_permalink": obj.forum_permalink,
            "phase": obj.phase,
            "in_retention": obj.in_retention
        }
    }

    if obj.upper_arch:
        data['case']['upper_arch'] = {
            "uuid": obj.upper_arch.uuid.hex,
            "name": obj.upper_arch.name
        }

    if obj.lower_arch:
        data['case']['lower_arch'] = {
            "uuid": obj.lower_arch.uuid.hex,
            "name": obj.lower_arch.name
        }

    return data

处理数据的功能

{
  'Type': 'Notification',
  'MessageId': 'e4ad7fc2-1483-48d7-94be-624aa26b4fff',
  'TopicArn': '...',
  'Subject': 'Hello',
  'Message': {
    'type': 'ias.case.updated',
    'content': {
      'case': {
        'case_id': 'XO1GBRP',
        'uuid': 'f7c8799d793a426bad50fd1f325de6bf',
        'owner': {
          'uuid': '6f08398e91f14422bb4ac7dffb2e093a'
        },
        'status': {
          'uuid': '12d2277ed3c7450b9c65ee85ed0935a3',
          'message': 'processing',
          'extra_data': None
        },
        'patient': {
          'uuid': 'b9b964ea2ada4b049f8812e1c06259b1',
          'details': {
            'practice': {
              'uuid': 'e0ed87aacbc54fda85639c0d32d1c693',
              'name': ''
            }
          }
        },
        'start_date': '2019-01-14',
        'case_photos': [

        ],
        'forum_permalink': None,
        'phase': None,
        'in_retention': False,
        'upper_arch': {
          'uuid': '371435d0e36c45adb6e01c659472f3d8',
          'name': 'Inman Aligner'
        }
      }
    },
    'origin': 'ias_cs'
  },
}

跟踪

def get_case_status(status_data):
    defaults = {
        "status": status_data['message'],
        "status_message": generate_status_message(status_data['message']),
    }
    if status_data.get('extra_data'):
        defaults['extra_data'] = status_data['extra_data']

    status, created = CaseStatus.objects.get_or_create(
        uuid=status_data['uuid'],
        defaults = defaults
    )

    return status

1 个答案:

答案 0 :(得分:2)

在视图中检查status_data['message']。看起来好像是元组(<CaseStatus: CaseStatus object (13)>,)而不是状态对象<CaseStatus: CaseStatus object (13)>

您的代码中可能包含逗号,从而使status成为元组,例如:

status = CaseStatus(...),

尽管如此,我们不能告诉您,因为您没有包括完整的回溯或设置status_data的其余代码。