相关字段的查找无效:​​title_icontains

时间:2019-03-22 14:44:34

标签: python django class view django-views

我的模型。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)

我的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/<int:pk>/', ProductUpdateView.as_view(), name='update'),
    path('urunler/duzenle/<slug>/', ProductUpdateView.as_view(), name='update_slug'),
    path('urunler/<int:pk>/', ProductDetailView.as_view(), name='detail'),
    path('urunler/<slug>/', ProductDetailView.as_view(), name='detail_slug'),
    path('kategori/ekle/', CategoryCreateView.as_view(), name='add_category'),
    path('kategori/duzenle/<int:pk>/', CategoryUpdateView.as_view(), name='update_category'),
    path('kategori/duzenle/<slug>/', CategoryUpdateView.as_view(), name='update_slug_category'),


]

我正试图在产品详细信息页面上显示相关的库存记录。

当我访问产品详细信息页面时,出现此错误。“相关字段的查询无效:icontains” domain.com:8000/urunler/16(例如)

我需要更正ProductDetailView类和get_context_data函数,但如何:)

有帮助吗?

1 个答案:

答案 0 :(得分:3)

您已经将字段称为陌生名称,这使您感到困惑。 title不是标题,而是产品模型的外键,该模型本身具有title属性。

这将起作用:

data = Stocks.objects.filter(title__title__icontains=product_name).order_by('price')

但是实际上您应该将该字段重命名为一个明智的名称,例如product,以便您执行product__title__icontains