I'm building a search for my product price tracking app and there're 2 models products and scans
class products(models.Model):
enter code hereproduct_id = models.IntegerField(primary_key=True)
product_name = models.CharField(max_length=100)
product_link = models.CharField(max_length=255)
image_link = models.CharField(max_length=100)
store_name = models.CharField(max_length=100)
class Meta:
verbose_name_plural = "products"
def __str__(self):
return str(self.product_name)
class scans(models.Model):
scan_id = models.IntegerField(primary_key=True)
prod_id = models.IntegerField(models.ForeignKey("scrape.Model", on_delete=models.CASCADE))
price = models.IntegerField()
scanned_time = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name_plural = "scans"
def __repr__(self):
return str(self.prod_id)
the search is supposed to take a name(string) input and query for a store name in the same table and query for price which is in another table and return the query set with the product_name, store_name and price.
I've tried this
class searchResults(ListView):
model = 'products', 'scans'
template_name = 'result.html'
# let query = GET request from form (in home.html)
def get_queryset(self):
queryset = []
rawquery = self.request.GET.get('q')
queries = rawquery.split(" ")
for q in queries:
prodresults = products.objects.filter(
Q(product_name__icontains=q)
).distinct()
for prodresult in prodresults:
prod_id = products.objects.get(product_name=prodresult).product_id
prod_price = scans.objects.get(prod_id=prod_id).price
for result in prodresults:
#joins results toegther
queryset.append(result)
queryset.append(prod_price)
return list(set(queryset))
The code broke at
prod_price = scans.objects.get(prod_id=prod_id).price
with scans matching query does not exist.