在django中将request.POST数据绑定到表单时出现延迟
当尝试将 request.POST 数据绑定到表单时,我遇到了大约 20.-30 秒的无法解释的延迟:
CompanyFormset = modelformset_factory(Company, form=EditCompanyForm, extra=0)
if request.method == 'POST':
formset = CompanyFormset(request.POST)
if formset.is_valid():
formset.save()
它只发生在一台服务器上,无论我使用开发服务器还是 mod_wsgi。当在我的开发计算机上尝试时,一切都运行得足够快。如果我尝试通过手动分配值来保存模型,它应该会很快。
延迟只发生在这条线上:
formset = CompanyFormset(request.POST)
..我不知道什么会导致它或在哪里寻找问题原因。
有什么建议吗?
这也是模型:
class Company(models.Model):
name = models.CharField(_('name'),blank=True, max_length=200)
type_of_corporation = models.CharField(_('type of organization'),blank=True, max_length=100)
tax_number = models.CharField(_('tax number'),blank=True, max_length=100)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
logo = models.FileField(_('logo'),upload_to='bex/logos', blank=True, null=True)
description = models.TextField(_('description'),blank=True)
street = models.CharField(_('street'),blank=True, max_length=100)
city = models.CharField(_('city'),blank=True, max_length=100)
zip = models.CharField(_('zip'),blank=True, max_length=50)
region = models.CharField(_('region'),blank=True, max_length=100)
COUNTRIES = (
('Croatia', 'Croatia'),
('Slovenia', 'Slovenia'),
)
country = models.CharField(_('country'),max_length=20, choices=COUNTRIES)
telephone = models.CharField(_('telephone'),blank=True, max_length=20)
fax = models.CharField(_('fax'),blank=True, max_length=20)
email = models.EmailField(_('email'),blank=True)
website = models.URLField(_('website'),null=True, blank=True, verify_exists=False)
representing_person = models.CharField(_('representing person'), blank=True, max_length=100)
owner = models.ForeignKey(User, blank=True, null=True, verbose_name=_('owner'), related_name='company_owner')
owner_approved = models.BooleanField(blank=True)
company_approved = models.BooleanField(default=True)
business_category = models.ForeignKey(BusinessCategory, blank=True, null=True, verbose_name=_('business category'))
subscription_date = models.DateTimeField(_('subscription date'),blank=True, null=True)
score = models.IntegerField(null=True, blank=True)
votes = models.IntegerField(null=True, blank=True)
""" company data needs to have ratings, comments, category and business tags """
class Admin:
list_display = ('',)
search_fields = ('',)
save_as = True
class Meta:
verbose_name = _('Company')
verbose_name_plural = _('Companies')
def rating_allowed(self):
if not self.subscription_date:
try:
user = User.objects.get(id=self.owner.id)
if (datetime.today() - user.date_joined).days < 30:
rating_allowed = True
except:
rating_allowed = False
else:
if (datetime.today() - self.subscription_date).days < 365:
rating_allowed = True
else:
rating_allowed = False
self.rating_allowed = rating_allowed
return self.rating_allowed
def get_absolute_url(self):
return "/companies/%i/" % self.id
def __unicode__(self):
return self.name
I'm getting unexplainable delay of about 20.-30 seconds when trying to bind request.POST data to form:
CompanyFormset = modelformset_factory(Company, form=EditCompanyForm, extra=0)
if request.method == 'POST':
formset = CompanyFormset(request.POST)
if formset.is_valid():
formset.save()
It only happens on one server, no matter if I use development server or mod_wsgi. When trying on my development computer everything works fast enough. If I try to save model by manually assigning values it's fast as it should be.
Delay only happens on this line:
formset = CompanyFormset(request.POST)
..and I have no more ideas what can cause it or where to look for problem cause.
Any suggestions what to do?
Here is the model too:
class Company(models.Model):
name = models.CharField(_('name'),blank=True, max_length=200)
type_of_corporation = models.CharField(_('type of organization'),blank=True, max_length=100)
tax_number = models.CharField(_('tax number'),blank=True, max_length=100)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
logo = models.FileField(_('logo'),upload_to='bex/logos', blank=True, null=True)
description = models.TextField(_('description'),blank=True)
street = models.CharField(_('street'),blank=True, max_length=100)
city = models.CharField(_('city'),blank=True, max_length=100)
zip = models.CharField(_('zip'),blank=True, max_length=50)
region = models.CharField(_('region'),blank=True, max_length=100)
COUNTRIES = (
('Croatia', 'Croatia'),
('Slovenia', 'Slovenia'),
)
country = models.CharField(_('country'),max_length=20, choices=COUNTRIES)
telephone = models.CharField(_('telephone'),blank=True, max_length=20)
fax = models.CharField(_('fax'),blank=True, max_length=20)
email = models.EmailField(_('email'),blank=True)
website = models.URLField(_('website'),null=True, blank=True, verify_exists=False)
representing_person = models.CharField(_('representing person'), blank=True, max_length=100)
owner = models.ForeignKey(User, blank=True, null=True, verbose_name=_('owner'), related_name='company_owner')
owner_approved = models.BooleanField(blank=True)
company_approved = models.BooleanField(default=True)
business_category = models.ForeignKey(BusinessCategory, blank=True, null=True, verbose_name=_('business category'))
subscription_date = models.DateTimeField(_('subscription date'),blank=True, null=True)
score = models.IntegerField(null=True, blank=True)
votes = models.IntegerField(null=True, blank=True)
""" company data needs to have ratings, comments, category and business tags """
class Admin:
list_display = ('',)
search_fields = ('',)
save_as = True
class Meta:
verbose_name = _('Company')
verbose_name_plural = _('Companies')
def rating_allowed(self):
if not self.subscription_date:
try:
user = User.objects.get(id=self.owner.id)
if (datetime.today() - user.date_joined).days < 30:
rating_allowed = True
except:
rating_allowed = False
else:
if (datetime.today() - self.subscription_date).days < 365:
rating_allowed = True
else:
rating_allowed = False
self.rating_allowed = rating_allowed
return self.rating_allowed
def get_absolute_url(self):
return "/companies/%i/" % self.id
def __unicode__(self):
return self.name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模型中一切看起来都很好。您可以尝试出色的 django 调试工具栏,它会报告时间,并可以帮助您确定它是什么花了这么长时间。
Everything looks okay in the model. You could try the excellent django debug toolbar which reports timings, and may help you pinpoint what it's taking so long.