Search code examples
djangomodeldjango-querysetinherited

Django Inherited Model: "Cannot resolve keyword 'keyword' into field." (Django 2.1.1)


guys. Same Context Processor, new problem (linked to this question).

I have the following model to check for promotions on a website:

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")

That is inherited from this model:

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

On my context processor related to this pages, i've writen a code to request page promotions like this:

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

At first it was like the linked version, but i've tried to modify it since i was getting the following error:

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`

If you go to the previous question, you'll see the small difference between the codes. The problem seems to be that Django isn't recognizing the field associated with the inherited model, but i didnt get why. Any hints?


Solution

  • I have not solved this entirely, but the problem seemed to be at the URLField. Don't know if Django couldn't understand the field, but it was solved when I changed the field to CharField. I'm going to insert a validator on the field asking for URLField, but in case anyone comes to a similar problem, that's a way to solve it.