0

我有一个模型Order

class Order(SmartModel):
    SUBMITTED = 1
    PROCESSED = 2 
    DELIVERED = 3 
    PICKED_UP = 4
    CANCELLED = 5
    ORDER_STATUSES = ((SUBMITTED,'Submitted'),(PROCESSED,'Processed'),(DELIVERED,'Delivered'),(PICKED_UP,'picked_up'),(CANCELLED,'Cancelled'),
    date = models.DateTimeField(auto_now=True,auto_now_add=True)
    status = models.IntegerField(choices=ORDER_STATUSES, default=SUBMITTED)
    restaurant = models.ForeignKey(Restaurant,null=True,blank=True,default = None,help_text="The restaurant the customer order from")
    email = models.EmailField(max_length=50,help_text="Needed as alternative")
    mobile = PhoneNumberField(max_length=20,default='+25078######',help_text="Needed to communicate and confirm payment from mobile money")

我按如下方式创建并保存此对象,

def create_order(request,self):
    orders = Order()
    checkout_form = forms.CheckoutForm(request.POST,instance=orders)
    orders = checkout_form.save(commit=False)
    anon_user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
    orders.created_by = anon_user
    orders.modified_by = anon_user
    orders.status = Order.SUBMITTED
    orders.save()
        ......

我的管理员看起来像这样,

class OrderCRUDL(SmartCRUDL):
    model = Order
    permissions = True
    actions = ('list','read','create','delete','update')

    class List(SmartListView):
        fields = ('date','status','billing_name','mobile','email','billing_address','billing_city','restaurant','restaurant.services')
        search_fields = ('date',)

我想在管理员中restaurant查看仅从ordersWeb 应用程序的菜单中创建的列表。截至目前,餐厅可以查看应用程序上所有订单的列表。

我怎样才能做到这一点,也欢迎阅读建议。

这是正在使用的代码并出现错误TypeError: object of type 'instancemethod' has no len()

def queryset(self,request):
    queryset = self.Order.objects.get_queryset()
    current_restaurant = Restaurant.objects.get(id=request.user)
    return queryset.filter(restaurant = current_restaurant)
4

2 回答 2

0

尝试在您的管理类中使用查询集过滤器:

def queryset(self, request):
    qs = self.model._default_manager.get_query_set()
    current_restaurant = Restaurant.objects.get(id = request.user) #You need to get current restaurant instance
    return qs.filter(restaurant = current_restaurant)

您需要current_restaurant使用当前餐厅的登录数据获取对象。更多信息可以在IBM 文档中的清单 14中找到。

于 2013-03-07T10:03:16.313 回答
0

我设法通过做这样的事情来取得成果

def derive_queryset(self):
    queryset = super(OrderCRUDL.List, self).derive_queryset()
    #if super-user,show all comments
    if self.request.user.is_superuser:
       return queryset
    return queryset.filter(restaurant=self.request.user)
于 2013-03-07T20:31:27.950 回答