目前我正在使用django调试工具栏检查django查询(django-rest-framework)。 postgres数据库查询时间约为100毫秒即可。但响应时间大约需要6秒,这很慢。
我发现最慢(约需4秒)的功能就是:
@transaction.atomic
def create(self, *args, **kwargs):
response = super(PersonView, self).create(*args, **kwargs)
data = response.data
product_data = data['packages'][0]['product']
person = Person.objects.get(pk=data['id'])
if not person.info:
product = Product.objects.get(pk=product_data['id'])
PersonItem.objects.create(person=person, product=product)
response.data = PersonSerializer(person).data
return response
Any ideas why the response is so slow or why this function takes so long ?
修改: 这是我的PersonSerializer类。
class PersonSerializer(serializers.ModelSerializer):
person_items = PersonItemSerializer(many=True, read_only=True)
address = AddressSerializer(read_only=True)
office_address = serializers.SerializerMethodField('get_office_address')
status = ChoiceWithNameField(choices=Person.STATUS_CHOICES)
source = ChoiceWithNameField(
choices=Person.SOURCE_CHOICES, required=False)
closing_person = ClosingPersonSerializer(read_only=True)
is_upfront_person = serializers.BooleanField(read_only=True)
has_discount = serializers.BooleanField(read_only=True)
payment_method = serializers.CharField(read_only=True)
user_email = serializers.CharField(
read_only=True, source='user.email')
user_display_name = serializers.CharField(
read_only=True, source='user.display_name')
billing_display_address = serializers.SerializerMethodField(
'get_billing_display_address')
real_estate_display_address = serializers.SerializerMethodField(
'get_real_estate_display_address')
net_total = serializers.DecimalField(read_only=True, source='net_total')
gross_total = serializers.DecimalField(read_only=True, source='gross_total')
tax = serializers.DecimalField(read_only=True, source='tax_total')
coupon_net_total = serializers.DecimalField(read_only=True,
source='coupon_net_total')
coupon_gross_total = serializers.DecimalField(read_only=True,
source='coupon_gross_total')
class Meta:
model = Person
policy_class = PersonPolicy
def get_billing_display_address(self, obj):
try:
address = obj.address
except Address.DoesNotExist:
pass
else:
return "{0} {1}, {2}, {3}".format(
address.house_name_number, address.address_2, address.town,
address.postcode)
def _filter_products(self, products):
user = getattr(self.context.get('request'), 'user', None)
if not user or not user.is_staff:
products = products.filter(admin_only=False)
return products
def _as_person_items(self, person, products):
persons = [PersonItem(name=p.name, description=p.description,
person=person,
price=p.total_up_front,
product=p) for p in products]
return persons
def get_packages(self, person):
products = self._filter_products(person.packages)
return PersonItemMinimalSerializer(
self._as_person_items(person, products),
many=True).data
def get_office_address(self, i):
try:
office_address = i.addresses.get(address_type=Address.ADDRESS)
except Address.DoesNotExist:
return False
return AddressSerializer(office_address, read_only=True).data
def get_real_estate_display_address(self, obj):
try:
address = obj.office_address
except Address.DoesNotExist:
pass
else:
return "{0} {1}, {2}, {3}".format(
address.house_name_number, address.address_2,
address.town, address.postcode)