伙计们。相同的上下文处理器,新问题(链接到这个问题)。
我有以下模型来检查网站上的促销活动:
class PagePromotion(LinkedPromotion):
"""
A promotion embedded on a particular page.
"""
page_url = URLField(max_length=128, min_length=0)
def __str__(self):
return "%s on %s" % (self.content_object, self.page_url)
def get_link(self):
return reverse('promotions:page-click',
kwargs={'page_promotion_id': self.id})
class Meta(LinkedPromotion.Meta):
verbose_name = _("Page Promotion")
verbose_name_plural = _("Page Promotions")
这是从此模型继承的:
class LinkedPromotion(models.Model):
# We use generic foreign key to link to a promotion model
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = fields.GenericForeignKey('content_type', 'object_id')
position = models.CharField(_("Position"), max_length=100,
help_text="Position on page")
display_order = models.PositiveIntegerField(_("Display Order"), default=0)
clicks = models.PositiveIntegerField(_("Clicks"), default=0)
date_created = models.DateTimeField(_("Date Created"), auto_now_add=True)
class Meta:
abstract = True
app_label = 'promotions'
ordering = ['-clicks']
verbose_name = _("Linked Promotion")
verbose_name_plural = _("Linked Promotions")
def record_click(self):
self.clicks += 1
self.save()
record_click.alters_data = True
在与此页面相关的上下文处理器上,我编写了一个代码来请求这样的页面促销:
def get_request_promotions(request):
"""
Return promotions relevant to this request
"""
promotions = PagePromotion.objects.filter(page_url=request.path).order_by('display_order')
if 'q' in request.GET:
keyword_promotions \
= KeywordPromotion.objects.select_related().filter(keyword=request.GET['q'])
if keyword_promotions.exists():
promotions = list(chain(promotions, keyword_promotions))
return promotions
起初它就像链接版本,但我尝试修改它,因为我收到以下错误:
Cannot resolve keyword 'page_url' into field.
Choices are: clicks, content_object, content_type, content_type_id,
date_created, display_order, id, object_id, position`
如果您转到上一个问题,您会看到代码之间的细微差别。问题似乎是 Django 没有识别与继承模型关联的字段,但我不明白为什么。有什么提示吗?