user3639459
user3639459

Reputation: 1

Django - filtering on foreign key

I have a problem about filter in django. Please help me. I want to display the objects of the product which has different categories when I click on l.category_name

my html (CategoryList.html):

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

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

Upvotes: 0

Views: 390

Answers (2)

DeeGautam
DeeGautam

Reputation: 41

The way foreign keys are stored is through automatic fields(IDs). 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.

I think your code is a little confusing since you are trying to do somethings django does automatically for you. 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.

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_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

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.

Something like this: Entry.objects.all().update(blog=b)

It will be well worth your time reading through the queries help. Django queries

Upvotes: 0

user2867522
user2867522

Reputation:

There seem to be a lot of problems with your code.

First, you don't have to declare ids in your code. Django does that automatically for you. So, categor_id and product_id are unnecessary.

Second, Remove the .POST check. 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. Use the {% url %} tag instead.

Finally, You can then pass this product_list to the template

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

Upvotes: 3

Related Questions