0
I’m having a problem creating a page to create new products and know I’m having some problem with Oop for lack of experience. I would like to pass the value of self
in this case being self==Product
, but eventually passed self==Creat_view
and I don’t know what I’m doing wrong. (the indentation of the classes are correct in the program(-1 tab), only the code that doesn’t stick right here)
form_class_mapping = {'Product':ProductModelForm,'Gondola':ProdGondolaModelForm,'Stock':ProdStockModelForm}
class List_view(ListView):
template_name ='templates/list.html'
def __init__(self):
print(self)
queryset = self.objects.all()
class Creat_view(CreateView):
template_name ='templates/create.html'
def __init__(self):
queryset = self.objects.all()
self.form_class = form_class_mapping[self]
def form_valid(self,form):
return super().form_valid(form)
Here are my urls:
app_name = 'Product'
urlpatterns = [
path('', List_view.as_view(),name="Product-list"),
path('create/', Creat_view.as_view(),name="Product-create"),
path('<int:id>/update/', Update_view.as_view(),name="Product-update"),
path('<int:id>/delete/', Delete_view.as_view(),name="Product-delete")
]
And my mistake is:
File "C:\Users\user\dev\cms\sgi\StockManagement\views.py", line 22, in __init__
queryset = self.objects.all()
AttributeError: 'Creat_view' object has no attribute 'objects'
Edit: I can make it work if I change my code to (below), but I would like to use this class for other models besides Product
class Creat_view(CreateView):
template_name ='templates/create.html'
def __init__(self):
queryset = Product.objects.all()
form_class = ProductModelForm
def form_valid(self,form):
return super().form_valid(form)
This answers your question? Why do we have to use the self attribute as an argument in the methods?
– Augusto Vasques
It helped me understand the self argument, but I still don’t know how to solve my problem. I can make it work if I change as I added in the edit, but I would like to use this class to access other models besides Product and do not know how to pass the argument into the function without an error that the number of arguments is wrong.
– Eduardo B
What you just explained to me is enlightening. PS: I ask why website format that requires relevant information to be contained in the question, the comments are only a disposable medium to improve the questions.
– Augusto Vasques