属性错误Nonetype具有o属性“名称”

时间:2019-08-01 05:50:35

标签: python flask attributes

烧瓶的新手,我不确定为什么会出现此名称错误:“非类型”对象没有属性值“名称”。

(请忽略:“您的帖子似乎主要是代码;请添加更多详细信息。 看起来您的帖子大部分是代码;请添加更多详细信息。 看起来您的帖子大部分是代码;请添加更多详细信息。 “)

这是控制台中的外观

  File "/Users/thomashunt/projects/ct-platform-api/apis/student_api.py", line 448, in put
    return StudentService.student_WorkShadow(submission)
  File "/Users/thomashunt/projects/ct-platform-api/services/students.py", line 234, in student_WorkShadow
    AddressService.set_address_info(submission.student_detail.location_address)
  File "/Users/thomashunt/projects/ct-platform-api/services/addresses.py", line 18, in set_address_info
    address_description = address.address_description(country.name)
AttributeError: 'NoneType' object has no attribute 'name'

服务/学生

@staticmethod 
    def student_WorkShadow(submission: StudentWorkShadowEdit) -> Person:
        repo = PersonData()
        advisor = repo.find_by_email(submission.advisor_email)
        email = submission.email.lower()
        student = repo.find_by_email(email)

        if not student:
            raise RecordNotFoundException('No Record with this email in the database')

        if not advisor:
            raise RecordNotFoundException('No Record with this advisor email in the database')

              # Forced re-write of Address entered by Student
        student.student_detail.location_address = \
            AddressService.set_address_info(submission.student_detail.location_address)


        submission.set_model(student)

        files = StudentService.promote_student_files(advisor, submission.file_ids, student.id)

                # Forced re-write of Address entered by Student


        repo.save(student, advisor.id)
        repo.session.commit()
        student_statement = 'student workshadow details updated'
        reference_fields = [EventItemReferenceField('updated_workshadowDetails', 'Updated workshadow Details'),
                            EventItemReferenceField('form_action', 'confidential_updated')]
        reference_content = [student_statement]

        MessagingActivityService.create_student_event_for_action(student.id, None, student,
                                                                 True,
                                                                 ActionTypes.Student.value.InternalNote,
                                                                 student_statement,
                                                                 reference_fields,
                                                                 reference_content, files, None,
                                                                 None, None, True, True)

        StudentService.re_index(student)
        return student

API端点

@ns.route('/StudentWorkShadow')
class StudentWorkShadowEndpoint(Resource):
    @SecurityService.requires_system
    @ns.expect(student_workshadow_model, validate=True)
    @ns.marshal_with(student_person_model)
    def put(self):
        logging.info('student workshadow details updated')
        submission = StudentWorkShadowEdit.from_dict(request.json)
        return StudentService.student_WorkShadow(submission)

服务/地址

import logging

from models import Address
from resources import AddressEdit
from utility import GoogleUtility
from .data import CountryData


class AddressService:
    @staticmethod
    def set_address_info(address: Address):
        countries = CountryData()
        country = countries.load_country(address.country_code)

        if address.suburb is not None and address.state is not None:
            address.location_description = address.suburb + ', ' + address.state

        address_description = address.address_description(country.name)

        maps_result = GoogleUtility.resolve_coords(address_description)
        try:
            first_result = maps_result[0]
            print(first_result)
            address.latitude = first_result['geometry']['location']['lat']
            address.longitude = first_result['geometry']['location']['lng']
            address.raw_location = first_result
            address.formatted_address = first_result['formatted_address']
        except TypeError:
            print(maps_result.error)
            logging.error(maps_result.error)
        except IndexError:
            logging.error('No result for address resolution')
        return address

    @staticmethod
    def has_address_changed(old_address: Address, new_address: AddressEdit):
        if not old_address and new_address:
            return True
        return not (old_address.line_1 == new_address.line_1
                    and old_address.line_2 == new_address.line_2
                    and old_address.suburb == new_address.suburb
                    and old_address.postcode == new_address.postcode
                    and old_address.country_code == new_address.country_code)

国家/数据输出:

import json

from resources import Country


class CountryData:
    with open('services/data/countries.json') as json_data:
        source = json.load(json_data)
        countries = [Country.from_dict(l) for l in source]

    def load_country(self, country_code: str):
        result = None
        for country in self.countries:
            if country.country_code == country_code:
                result = country
        return result

    def load_state(self, country_code: str, short_title: str):
        result = None
        country = self.load_country(country_code)
        for state in country.states:
            if state.short_title == short_title:
                result = state
        return result

    def list_states(self, country_code: str):
        return self.load_country(country_code).states

1 个答案:

答案 0 :(得分:0)

我怀疑您为func (s *DocumentStore) NewSignedGetURL(ctx context.Context, objectKey string, ttl time.Duration) (string, error) { svc := s3.New(s.aws) req, _ := svc.GetObjectRequest(&s3.GetObjectInput{ Bucket: aws.String(s.bucketName), Key: aws.String(objectKey), }) url, err := req.Presign(ttl) if err != nil { return "", fmt.Errorf("failed to presign GetObjectRequest for key %q: %v", objectKey, err) } return url, nil } 传递的值与任何getURL, err := target.NewSignedGetURL(context.TODO(), result.ObjectKey, time.Minute*5) if err != nil { t.Errorf("failed to sign url: %v", err) return } req, _ := http.NewRequest("GET", getURL, nil) req.Header.Add("host", req.Host) resp, err := http.DefaultClient.Do(req.WithContext(context.TODO())) if err != nil { t.Errorf("failed to request object from signed url: %v", err) return } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { t.Errorf("failed to read object stream from S3: %v", err) return } 属性都不匹配。

我的建议是在这样的方法中放入调试打印行:

country_code

这样做,您应该能够查看结果是否设置为country.country_code以外的值,并且可以准确观察到哪个国家/地区代码触发了该结果。此外,这将打印所有可用的国家代码(每行一个)。如果您的class CountryData: ... def load_country(self, country_code: str): result = None for country in self.countries: if country.country_code == country_code: result = country print(result, country.country_code) # this line added return result ... 不是其中之一,那就是问题。