简体   繁体   English

Django-过滤外键

[英]Django - filtering on foreign key

I have a problem about filter in django. 我在Django中有一个关于filter的问题。 Please help me. 请帮我。 I want to display the objects of the product which has different categories when I click on l.category_name 当我单击l.category_name时,我想显示具有不同类别的产品对象

my html (CategoryList.html): 我的html(CategoryList.html):

{% for l in forms %}
<a href="/myapp/categorylist/{{l.category_id}}"><h2>{{ l.category_name }}</h2></a>
{% endfor %}

CategoryView.html CategoryView.html

{{get_product.product_name}}

my model: 我的模特:

class Category(models.Model):
    category_id = models.AutoField(primary_key = True)
    category_name = models.CharField(max_length = 20)

    def __unicode__(self):
        return self.category_name

class Product(models.Model):
    product_id = models.AutoField(primary_key = True)
    product_name = models.CharField(max_length = 50)
    product_category = models.ForeignKey(Category)
    product_color = models.CharField(max_length = 30)
    def __unicode__(self):
        return self.product_name

my view: 我的观点:

def category_list(request):
    list = Category.objects.all()
    context = {'forms':list}
    return render(request,'webpage/CategoryList.html',context)

def category_view(request,category_id):
    all = Product.objects.all()
    if request.POST:
        get_id = Category.objects.get(category_id = request.POST['category_id'])
        get_category = Product.objects.get(product_category =
                                           request.POST['product_category'])
        get_category.product_category = get_id
        get_category.save()
        if get_category:
            get_product = Product.objects.filter(product_category__category_name =
                                                 request.POST['category_name'])

    context = {'get_product':get_product}
    return render(request,'webpage/CategoryView.html',context)

I read document in https://docs.djangoproject.com/en/1.6/topics/db/queries/ but i don't understand .I know i was wrong category_view 我在https://docs.djangoproject.com/en/1.6/topics/db/queries/中阅读了文档,但我听不懂。我知道我错了category_view

There seem to be a lot of problems with your code. 您的代码似乎有很多问题。

First, you don't have to declare ids in your code. 首先,您不必在代码中声明ID。 Django does that automatically for you. Django会自动为您执行此操作。 So, categor_id and product_id are unnecessary. 因此, categor_idproduct_id是不必要的。

Second, Remove the .POST check. 其次,删除.POST检查。 You aren't posting anything. 您什么都没张贴。

Third, 第三,

get_id = Category.objects.get(category_id = request.POST['category_id']) # returns a category, not an id
get_category = Product.objects.get(product_category =
                                       request.POST['product_category']) # returns the product list, not a category
get_category.product_category = get_id

is the same as 是相同的

category = Category.objects.get(category_id = request.POST['category_id'])
product_list = Product.objects.get(product_category = category)

Fourth, don't hardcode URLs in your template. 第四,不要在模板中对URL进行硬编码。 Use the {% url %} tag instead. 请改用{% url %}标签。

Finally, You can then pass this product_list to the template 最后,您可以将该product_list传递给模板

context = {'product_list':product_list}
return render(request,'webpage/CategoryView.html',context)

The way foreign keys are stored is through automatic fields(IDs). 外键的存储方式是通过自动字段(ID)。 Since 'Category' is a foreign field of 'Product', when you make a record entry, the id of category is stored in 'product_category' field in products table. 由于“类别”是“产品”的外来字段,因此当您进行记录条目时,类别的ID存储在产品表的“产品类别”字段中。

I think your code is a little confusing since you are trying to do somethings django does automatically for you. 我认为您的代码有些混乱,因为您正在尝试执行django为您自动完成的操作。 Like, once you define a foreign key, the id of the foreign key table record is stored automatically, you don't have to get the id of 'category' entry and store it in products table entry. 就像,一旦定义了外键,外键表记录的ID就会自动存储,您不必获取“类别”条目的ID并将其存储在产品表条目中。

What you are trying to achieve is simple, lets say you have the category_name and nothing else, get the id of the category table entry, 您想要实现的目标很简单,可以说您拥有category_name,而没有别的,获取category表条目的id,

category_object = Category.objects.get(category_name = category_name)
category_id = category_object .id

If you already have the ID of category, then you can skip the above step, and simply use the ID to query the products table to get the needed records 如果您已经具有类别的ID,则可以跳过上述步骤,只需使用ID查询产品表即可获取所需的记录。

Product.objects.filter(product_category = category_id)

In your templates, you can iterate through these product records and display whatever is needed. 在模板中,您可以遍历这些产品记录并显示所需的内容。

BTW, use the .update() method to update any fields instead of save() method. 顺便说一句,使用.update()方法更新任何字段,而不是save()方法。

Something like this: Entry.objects.all().update(blog=b) 像这样:Entry.objects.all()。update(blog = b)

It will be well worth your time reading through the queries help. 值得您花时间阅读查询帮助。 Django queries Django查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM