I trying to to create a database panel for a restaurant app where a user can add, remove, view and create a menu for the restaurant. In this the user can add the name, cuisine, category(veg/non veg) and cost as details of the menu item.
Here is my Models.py:
class Cuisine(models.Model):
cuisine = models.CharField(max_length=15)
def __str__(self):
return self.cuisine
class Meta:
verbose_name_plural = 'cuisines'
class Category(models.Model):
category_type = models.CharField(max_length=9)
def __str__(self):
return self.category_type
class Meta:
verbose_name_plural = 'category type'
class Item(models.Model):
name = models.CharField(max_length=50)
cost = models.IntegerField()
cuisine = models.ForeignKey(Cuisine, on_delete=models.CASCADE)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
def __str__(self):
return self.name
Here is Forms.py:
class ItemForm(forms.ModelForm):
class Meta:
model = Item
fields = ['name', 'cost']
class CategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ['category_type']
class CuisineForm(forms.ModelForm):
class Meta:
model = Cuisine
fields = ['cuisine']
And below is my Views.py:
@login_required()
def add_item(request):
if request.method == 'POST':
itemform = ItemForm(request.POST or None)
catform = CategoryForm(request.POST or None)
cuisineform = CuisineForm(request.POST or None)
if itemform.is_valid():
item = itemform.save(commit=False)
ct = catform.save(commit=False)
cui = cuisineform.save(commit=False)
item.name = request.POST.get('name')
cui.cuisine = request.POST.get('cuisine')
ct.category = request.POST.get('category_type')
item.cost = request.POST.get('cost')
item.save()
ct.save()
cui.save()
messages.success(request, 'Item added successfully!!')
else:
messages.warning(request, 'Unable to add the item.')
return redirect('add_item')
else:
item_list = ItemList.objects.all()
category = Category.objects.all()
cuisine = Cuisine.objects.all()
context = {
'items': item_list,
'category': category,
'cuisine': cuisine
}
return render(request, 'add_item.html', context)
I am able to add add the menu item from the django admin panel but i am having a error while saving the data to database from the user side as i want to give user the authority to add the item. I am not able to save the form using .save() option.
I know the error is in my forms.py but i don't know what as i am new to Django. I am attaching the screenshot of the error as well as of my frontend page from where i am adding the data.
Please note that i want to use Cuisine and Category as the dropdown list from the database.
- category_type
- This field is required.
` . Thus this error means that the code is not able to fetch the value ? – Paritosh Chaudhary Mar 06 '20 at 09:14