Reputation: 23
Here is my models.py:
class Product(models.Model):
name = models.CharField(max_length=200, db_index=True)
created_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
class Order(models.Model):
name = models.CharField(max_length=30)
created_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class OrderItem(models.Model):
order = models.ForeignKey(Order, related_name='items', on_delete=models.CASCADE)
product = models.ForeignKey(Product, related_name='order_items', on_delete=models.CASCADE)
schedule = models.CharField(max_length=7)
views.py:
def order_list(request):
orders = OrderItem.objects.filter(id__product=id, product_id=Product.objects.filter(created_by=request.user))
return render(request, 'orders/order/order_list.html', {'orders': orders})
Template:
{% block content %}
<div class="container">
<h4 class="text-center logo my-4">
<a>Queue List</a>
</h4>
<div>
<table class="table">
<thead class="thead-inverse">
<tr>
<th>Name</th>
<th>Gender</th>
<th>Birth Date</th>
<th>Age</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for order in orders %}
<tr>
<td>{{ order.order.name }}</td>
<td>{{ order.order.gender }}</td>
<td>{{ order.order.birth_date }}</td>
<td>{{ order.order.age }}</td>
<td><a href="" class="btn btn-primary mb-3" role="button">Add</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
I'm new to Django. I'd like to create view based on user who created the product. So when that user logs in to the web they can see the order item list.
Here is the error message:
Unsupported lookup 'product' for AutoField or join on the field not permitted.
Upvotes: 2
Views: 2023
Reputation: 1
The solution should be:
ModelName.objects.filter(created_by=self.request.user)
Upvotes: 0
Reputation: 106543
You should use the in
operator for your query filter instead of an exact match:
orders = OrderItem.objects.filter(product_id__in=Product.objects.filter(created_by=request.user))
Upvotes: 1