我有一个模型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
的管理员中查看仅通过网络应用中的菜单创建的orders
列表。截至目前,餐厅可以查看应用程序上所有订单的列表。
我如何实现这一点,也欢迎阅读建议。
这是代码使用并收到错误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)
答案 0 :(得分:0)
尝试在管理类中使用queryset过滤器:
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
个对象。更多信息可以在documentation by IBM中的清单14 中找到。
答案 1 :(得分: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)