0

I'm building a form that allows the user to add the different features of a product, and I also want it to be a dynamic form so the user can put whatever categories he want in it. Something like,

Form:

Name: shirt

Description: ...

Categorie1: Color: Red

Categorie2: Sleeve: short

Categorie3: ...

Add new categorie

Save

The problem is that I'm using CreateView for this page and don't know how to convert my form into a dynamic one. There's a way for doing this? or it's better to make it with a function view instead of CreateView?

Here's my code,

view:

class ProductCreateView(CreateView):
    template_name = "product_create.html"
    form_class = ProductModelForm
    queryset = Product.objects.all()

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

model:

class Product(models.Model):
    title = models.CharField(max_length = 120)
    description = models.TextField()
    image = models.FileField(upload_to="products/images/")
    price = models.IntegerField()
    active = models.BooleanField(default = True)
    categorie = models.CharField(max_lenght = 50, default = '')

    def get_absolute_url(self):
        return reverse("products:product-detail", kwargs={"id": self.id})

    def delete(self, *args, **kwargs):
        self.image.delete()
        super().delete(*args,**kwargs)

form:

class ProductModelForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = [
            'title',
            'description',
            'image',
            'price',
            'active',
            'categorie'
            ]

Thank you in advance!

motyas
  • 1
  • 2

1 Answers1

0

If I understood you correctly, what you want to do is to pass some fields without filling them. If this is the case, there are basically 2 ways of doing it. You can either overwrite the fields you want to be not required or overwrite the initialization of form class.

For the first case:

class ProductModelForm(forms.ModelForm):
    title = forms.CharField(required=False)
    # ... follow this for all fields you want to be not required ...

    class Meta:
        model = Product
        fields = [
            'title',
            'description',
            'image',
            'price',
            'active',
            'categorie'
            ]

For the second case:

class ProductModelForm(forms.ModelForm):
    
    class Meta:
        model = Product
        fields = [
            'title',
            'description',
            'image',
            'price',
            'active',
            'categorie'
            ]

    def __init__(self, *args, **kwargs):
        super(My_Form, self).__init__(*args, **kwargs)
        self.fields['title'].required = False
        # ... follow this for all fields you want to be not required ...

If you are having Foreign Keys in your model fields, latter solution is much better since you don't need to set qs attribute.

Note that your fields in your model should be both blank=True and null=True for these solutions to work

Rustam Garayev
  • 2,632
  • 1
  • 9
  • 13