Django 继承模型:"Cannot resolve keyword 'keyword' into field." (Django 2.1.1)
Django Inherited Model: "Cannot resolve keyword 'keyword' into field." (Django 2.1.1)
伙计们。相同的上下文处理器,新问题(链接到 )。
我有以下模型来检查网站上的促销活动:
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 没有识别与继承模型相关的字段,但我不明白为什么。有什么提示吗?
我还没有完全解决这个问题,但问题似乎出在 URLField 上。不知道Django是不是看不懂字段,我把字段改成CharField就解决了。我将在请求 URLField 的字段上插入一个验证器,但万一有人遇到类似问题,这是解决问题的一种方法。
伙计们。相同的上下文处理器,新问题(链接到
我有以下模型来检查网站上的促销活动:
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 没有识别与继承模型相关的字段,但我不明白为什么。有什么提示吗?
我还没有完全解决这个问题,但问题似乎出在 URLField 上。不知道Django是不是看不懂字段,我把字段改成CharField就解决了。我将在请求 URLField 的字段上插入一个验证器,但万一有人遇到类似问题,这是解决问题的一种方法。