Django. I can’t pass the model I’d like through classes

Asked

Viewed 51 times

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)
  • 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.

  • 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.

1 answer

0

After some time trying to solve I finally got it

form_class_mapping = {'Product':ProductModelForm,'Gondola':ProdGondolaModelForm,'Stock':ProdStockModelForm}
queryset_mapping = {'Product':Product,'Gondola':Product_Gondola,'Stock':Product_Stock}
class List_view(ListView):
    template_name ='templates/list.html'
    form_class = 0
    queryset = 0

    @classmethod
    def Argument(cls,*arg):
        cls.form_class = form_class_mapping[arg[0]]
        cls.queryset =  queryset_mapping[arg[0]].objects.all()


class Creat_view(CreateView):
    template_name ='templates/create.html'
    form_class = 0
    queryset = 0

    @classmethod
    def Argument(cls,*arg):
        cls.form_class = form_class_mapping[arg[0]]
        cls.queryset =  queryset_mapping[arg[0]].objects.all()

    def form_valid(self,form):
        print(form.cleaned_data)
        return super().form_valid(form)

the urls:

app_name = 'Product'

List_view.Argument(app_name)
Creat_view.Argument(app_name)
Update_view.Argument(app_name)
Delete_view.Argument(app_name)

print(List_view.__dict__)
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")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.