我创建了一个模型,并使用了随机生成器。因此,我必须为该模型使用UserManager。 Wen我在视图中使用此模型,但出现上述错误。我已经做了多次,但从未出错。我不知道怎么了请帮忙。预先感谢!
模型
def unique_rand():
while True:
code = Inventory.objects.make_random_password(length=6, allowed_chars="1234567890")
if not Inventory.objects.filter(code=code).exists():
return code
class Inventory(models.Model):
premises = models.ForeignKey(Premises, on_delete = None)
item = models.CharField(max_length=255,blank=False,unique=True)
code = models.CharField(max_length=255, null=True, blank=True,default = unique_rand)
quantity = models.IntegerField(null = True, blank=True)
is_defected = models.BooleanField(default = False)
objects = UserManager()
def __str__(self):
return self.item
观看次数
class Inventory(ListView):
model = Inventory
template_name = 'inventory.html'
def get_queryset(self):
pre = Premises.objects.get(userprofile__user = self.request.user)
queryset = Inventory.objects.filter(premises = pre)
return queryset
编辑:这是回溯
Internal Server Error: /operations/Inventory/
Traceback (most recent call last):
File "/home/urvi/qwenv/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/urvi/qwenv/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/urvi/qwenv/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/urvi/qwenv/local/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/urvi/qwenv/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/urvi/qwenv/local/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/urvi/qwenv/local/lib/python3.6/site-packages/django/views/generic/list.py", line 142, in get
self.object_list = self.get_queryset()
File "/home/urvi/Desktop/atventus-work-spaces/operations/views.py", line 34, in get_queryset
queryset = Inventory.objects.filter(premises = pre)
AttributeError: type object 'Inventory' has no attribute 'objects'
[07/Aug/2019 18:28:06] "GET /operations/Inventory/ HTTP/1.1" 500 90758
[07/Aug/2019 18:28:06] "GET /favicon.ico HTTP/1.1" 200 5670
我已经导入了模型和Usermanager。
答案 0 :(得分:1)
您已将视图命名为与模型相同的名称。重命名视图。