0

good day, please guys im working on a project whereby a vendor have to enter category of food they have so when i tried to add the same Category of food for two different vendors im seeing this error, to my own perspective i thought two vendors should be able to add the same Category of food.....................................................................

Models.py

   class Category(models.Model):
    vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE)
    category_name = models.CharField(max_length=50)
    slug = models.SlugField(max_length=100, unique=True)
    description = models.TextField(max_length=250, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'

    def clean(self):
        self.category_name = self.category_name.capitalize()

    def __str__(self):
        return self.category_name

views.py

    @login_required(login_url='login')
@user_passes_test(check_role_vendor)
def AddCategory(request):
    if request.method == 'POST':
        category_form = CategoryForm(request.POST)
        if category_form.is_valid():
            category_name = category_form.cleaned_data['category_name']
            category = category_form.save(commit=False)
            category.vendor = get_vendor(request)
            category.save()
            category.slug = slugify(category_name)+'-'+str(category.id)
            category.save()
            messages.success(request, 'Category added successfully')
            return redirect('menu-builder')
        else: 
            messages.error(request, 'Data already exist')
    else:
        category_form = CategoryForm()

    context = {
        'category_form': category_form,
    }
    return render(request, 'vendor/add-category.html', context)

template

    <!-- form -->
                            <form action="{% url 'add-category' %}" method="post">
                                {% csrf_token %}
                                <div class="form-fields-set">
                                    <div class="row">
                                        <div class="col-lg-12 col-md-12 col-sm-12">
                                            <div class="field-holder">
                                                <label>Category Name *</label>
                                                {{category_form.category_name}}
                                            </div> 
                                        </div>

                                        <div class="col-lg-12 col-md-12 col-sm-12">
                                            <div class="field-holder">
                                                <label>Description</label>
                                                {{category_form.description}}
                                            </div> 
                                        </div>

                                    </div>
                                </div>

forms.py

    class CategoryForm(forms.ModelForm):
    class Meta:
        model = Category
        fields = ['category_name', 'description']
  • Did you add `unique=True` to the `category_name`? – Willem Van Onsem Oct 17 '22 at 12:50
  • Please add the code of your model, in order to debug this error – Ahmad Othman Oct 17 '22 at 12:59
  • My models.py is there already I added it to the question. class Category(models.Model): vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE) category_name = models.CharField(max_length=50) slug = models.SlugField(max_length=100, unique=True) description = models.TextField(max_length=250, blank=True) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: verbose_name = 'category' verbose_name_plural = 'categories' – Azeez Soliudeen Oct 17 '22 at 14:14
  • @WillemVanOnsem no I didn’t add unique=True. I only add unique=True to the category_slug – Azeez Soliudeen Oct 17 '22 at 14:19

0 Answers0