django 无法将关键字“ir”解析到字段中,选项包括:分类,category_id,date,description,id,is_on_main,name,price,url

vfh0ocws  于 2023-06-25  发布在  Go
关注(0)|答案(1)|浏览(79)

由于某种原因,它看不到IR场,尽管它存在。views.py

def index(request):
    data = (
        Product
        .objects.select_related('category')
        .filter(is_on_main=True)
        .values('pk','name','price','ir')
    )
    categ = Category.objects.all()
    return render(request,'magazin/index.html',{'data' : data,'cat' : categ})

models.py(产品)

class Product(models.Model):
    is_on_main = models.BooleanField(default=False)
    category = models.ForeignKey(Category, on_delete = models.CASCADE)
    name = models.CharField(max_length=50)
    price = models.IntegerField()
    date = models.DateTimeField(null = True,auto_now=True)
    url = models.SlugField(max_length=100,unique=True,null=True)
    description = models.TextField(max_length=2000,default='Описание')
    ir = timezone.now() - datetime.timedelta(minutes=30)
    def __str__(self):
        return self.name
    
    class Meta:
        ordering = '-date',

html(part withdata

{% for i in data %}
        <div>
           <h4><b>НАЗВАНИЕ: </b>{{i.name}}</h4>
           <h4><b>ЦЕНА: </b>{{i.price}}</h4>
           <a href="{% url 'detail' pk=i.pk %}">ПОДРОБНЕЕ</a>
           {% if i.date >= i.ir %}
                <h1 class="r">НЕДАВНО В ПРОДАЖЕ!</h1> 
           {% endif %}                                                                                                                            
           <hr>                      
        </div>

先谢谢你了

gblwokeq

gblwokeq1#

Product模型中的ir声明无效。试着像这样声明:

ir = models.DateTimeField(default=(timezone.now() - datetime.timedelta(minutes=30)))

相关问题