Home > Enterprise >  I cannot retrieve the corresponding data when I want to relate the foreign key with the User object
I cannot retrieve the corresponding data when I want to relate the foreign key with the User object

Time:02-26

models.py

class Order(models.Model):
    PAYMENT_OPTIONS = (
        ('VISA','VISA'),
        ('Master','Master'),
        ('Octopus','Octopus'),
        ('Cash','Cash'),
    )
    STATUS = (
        ('Pending','Pending'),
        ('Delivered','Delivered'),
    )
    user = models.ForeignKey(User,models.CASCADE,null=True,blank=True)
    customer = models.CharField(max_length=200,null=True,blank=True)
    card_id = models.IntegerField(null=True,blank=True)
    mobile = models.IntegerField(null=True,blank=True)
    email = models.CharField(max_length=200,null=True,blank=True)
    
    total_price = models.DecimalField(decimal_places=2,max_digits=7)
    payment_method = models.CharField(max_length=50,choices=PAYMENT_OPTIONS,null=True,blank=True)
    status = models.CharField(max_length=50,choices=STATUS,default='Pending')
    date = models.DateTimeField(auto_now_add=True)
    address = models.CharField(max_length=350,null=True,blank=True)

    def __str__(self):
        return str(self.customer) '\'s Order'

views.py

def order_summary(request,order_id,user_id):
    user = User.objects.get(id=user_id)
    customer_order = Order.objects.get(id=order_id,user=user)
    food_order = OrderDetail.objects.filter(order=customer_order)
    context = {
        'customer_order':customer_order,
        'food_order':food_order}
    return render(request,'menu/order_summary.html',context)

urls.py

path('order_summary/<int:order_id>/<int:user_id>/',views.order_summary,name='order_summary'),

navbar.html

<li><a  href="{% url 'order_summary' [order_id]%}我的訂單</a></li>

Error:

Reverse for 'order_summary' with arguments '('', 1)' not found. 1 pattern(s) tried: ['order_summary/(?P<order_id>[0-9] )/(?P<user_id>[0-9] )/$']

For the above code, I may want to have the hyperlink that can access the order_summary method with filtering the correct User object. However, if I try to put the corresponding parameters that are corresponding to the user's order and the user.id, I received the error message even though I already included the url in the urls.py. Can anyone find out the problem if I want to refer the parameters in the navbar of HTML?

CodePudding user response:

You should add a user_id parameter in your urls.py file, so you can pass the currently logged in user (or any other user id you may desire).

So, your views would look like this:

def order_summary(request,order_id, user_id):
    user = User.objects.get(id=user_id)
    customer_order = Order.objects.get(id=order_id, user=user)
    food_order = OrderDetail.objects.filter(order=customer_order)
    context = {
        'customer_order':customer_order,
        'food_order':food_order}
    return render(request,'menu/order_summary.html',context)

And then in your templates, you can simply pass in the user id.

<li><a  href="{% url 'order_summary' [order_id] [user_id] %}我的訂單</a></li>

CodePudding user response:

I thinks you can directly get this by reverse relation you can try this,

request.user.order_set.all()

this will fetch the order data for current user.

  • Related