in Education by
My model.py: from django.conf import settings from django.db import models from django.db.models.signals import pre_save, post_save from django.urls import reverse from django.utils.text import slugify # Create your models here. class Stocks(models.Model): title = models.ForeignKey('Product', verbose_name="Ürün", on_delete=models.CASCADE) stock = models.DecimalField(max_digits=3,decimal_places=0,default=0,verbose_name="Stok") seller = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE) price = models.DecimalField(max_digits=100,decimal_places=2,default=0.00,verbose_name="Fiyat") def __str__(self): return str(self.title) class Meta: verbose_name = "Stok" verbose_name_plural = "Stoklar" def upload_image_location(instance, filename): return "%s/%s" %(instance.id, filename) class Product(models.Model): title = models.CharField(max_length=100,verbose_name="Başlık") slug = models.SlugField(blank=True, unique=True) category = models.ForeignKey('Category', null=True, blank=True, verbose_name="Kategori",on_delete=models.CASCADE) description = models.TextField(null=True,blank=True,verbose_name="Ürün Açıklaması") price = models.DecimalField(max_digits=100,decimal_places=2,default=0.00,verbose_name="Fiyat") sale_price = models.DecimalField(max_digits=100,decimal_places=2,default=0.00,null=True,blank=True,verbose_name="İndirimli Fiyat") tax = models.DecimalField(max_digits=3,default=18,decimal_places=0,verbose_name="KDV") status = models.BooleanField(default=True,verbose_name="Aktif/Pasif") image1 = models.FileField(blank=True,null=True,upload_to=upload_image_location,verbose_name="Vitrin Fotoğrafı") image2 = models.FileField(blank=True,null=True,upload_to=upload_image_location,verbose_name="Galeri Fotoğrafı 1") image3 = models.FileField(blank=True,null=True,upload_to=upload_image_location,verbose_name="Galeri Fotoğrafı 2") image4 = models.FileField(blank=True,null=True,upload_to=upload_image_location,verbose_name="Galeri Fotoğrafı 3") image5 = models.FileField(blank=True,null=True,upload_to=upload_image_location,verbose_name="Galeri Fotoğrafı 4") def get_absolute_url(self): view_name = "detail_slug" return reverse(view_name, kwargs={"slug": self.slug}) def __str__(self): return self.title class Meta: verbose_name = 'Ürün' verbose_name_plural = 'Ürünler' def create_slug(instance, new_slug=None): slug = slugify(instance.title) if new_slug is not None: slug = new_slug qs = Product.objects.filter(slug=slug) exists = qs.exists() if exists: new_slug = "%s-%s" %(slug, qs.first().id) return create_slug(instance, new_slug=new_slug) return slug def product_pre_save_reciever(sender, instance, *args, **kwargs): if not instance.slug: instance.slug = create_slug(instance) pre_save.connect(product_pre_save_reciever, sender=Product) My views.py class ProductDetailView(LoginRequiredMixin, MultiSlugMixin, DetailView): model = Product def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) product_name = self.object.title data = Stocks.objects.filter(title__icontains=product_name).order_by('price') context['stocks'] = data return context urls.py from django.urls import path from products import views from products.views import ( CategoryCreateView, CategoryUpdateView, ProductCreateView, ProductListView, ProductDetailView, ProductUpdateView, ) urlpatterns = [ path('urunler/', ProductListView.as_view(), name='list'), path('urunler/ekle/', ProductCreateView.as_view(), name='create'), path('urunler/duzenle//', ProductUpdateView.as_view(), name='update'), path('urunler/duzenle//', ProductUpdateView.as_view(), name='update_slug'), path('urunler//', ProductDetailView.as_view(), name='detail'), path('urunler//', ProductDetailView.as_view(), name='detail_slug'), path('kategori/ekle/', CategoryCreateView.as_view(), name='add_category'), path('kategori/duzenle//', CategoryUpdateView.as_view(), name='update_category'), path('kategori/duzenle//', CategoryUpdateView.as_view(), name='update_slug_category'), ] I'm trying to show related Stocks records on product detail page. When I visit product detail page I get this error."Related Field got invalid lookup: icontains" domain.com:8000/urunler/16 (for example) I need to correct ProductDetailView class and get_context_data function but how :) Any help? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You've called your fields strange names, which is confusing you. title is not a title, but a ForeignKey to the Product model, which itself has a title attribute. This would work: data = Stocks.objects.filter(title__title__icontains=product_name).order_by('price') but really you should rename that field to something sensible, eg product, so that you would do product__title__icontains.

Related questions

0 votes
    My model.py: from django.conf import settings from django.db import models from django.db.models.signals ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    In an election between two candidates, one got 55% of the total valid votes, 20% of the votes were invalid. If the total number of ... got, was : A) 2500 B) 2700 C) 2900 D) 3100...
asked Feb 14, 2021 in Education by JackTerrance
0 votes
    I am using Force.com Toolkit for PHP (Version 20.0) to integrate with Salesforce. I would like to look up ... this problem? Thanks! Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    Does there is any effect on particular neuron which got repeatedly fired ? (a) yes (b) no This question ... Neural Networks of Neural Networks Please answer the above question....
asked Sep 21, 2022 in Education by JackTerrance
0 votes
    Got recognition because of 73rd and 74th Amendments to the Constitution. (a) Parliament (b) Local self-governing ... (d) Cooperative societies Please answer the above question....
asked Aug 14, 2022 in Education by JackTerrance
0 votes
    I am learning about OAuth2 and OpenID Connect by experimenting with ASP.NET Core and IdentityServer4. So far ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 21, 2022 in Education by JackTerrance
0 votes
    I want to execute HelloWorldConsole.exe with an shipped Mono Framework through Go. So I want to call mono- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    How can I return the list in alphabets? I have sequence translator, and a python code that reads both ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    In my app I'm using a spinners to let the user select an item from a list. Everything works fine ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Hey guys sorry for posting that type of questions I was just got bored Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Explain the statement - We have got this Earth planet on lease from our future generations and ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 8, 2021 in Education by JackTerrance
0 votes
    In which year the Indian IT Act, 2000 got updated? (a) 2006 (b) 2008 (c) 2010 (d) 2012 I had been ... bank, Cyber Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    In the above DDL command the foreign key entries are got by using the keyword (a) References (b) ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    A number of cats got together and decided to kill between them 999919 rats. Every cat killed an equal number of rats. Each cat ... cat killed ? A) 1009 B) 991 C) 2000 D) 1000...
asked Feb 12, 2021 in Education by JackTerrance
...