i would like to make a structure database for a restaurant menu without using mptt or django-tree. here is my models.py:
-- from django.db import modelsclass Menu(models.Model):name = models.CharField(max_length=24, unique=True, verbose_name='menu name')#slug = models.SlugField(max_length=24, unique=True, help_text='The slug is the URL friendly version of the menu name, so that this can be accessed at a URL like mysite.com/menus/dinner/.')additional_text = models.CharField(max_length=128, null=True, blank=True, help_text='Any additional text that the menu might need, i.e. Served between 11:00am and 4:00pm.')order = models.PositiveSmallIntegerField(default=0, help_text='The order of the menu determines where this menu appears alongside other menus.')class Meta:ordering = ['name', 'order']class MenuCategory(models.Model):menu = models.ForeignKey(Menu, on_delete=models.CASCADE,help_text='The menus that this category belongs to, i.e. \'Lunch\'.')name = models.CharField(max_length=32, verbose_name='menu category name')additional_text = models.CharField(max_length=128, null=True, blank=True, help_text='The additional text is any bit of related information to go along with a menu category, i.e. the \'Pasta\' category might have details that say \'All entrees come with salad and bread\'.')order = models.IntegerField(default=0, help_text='The order is the order that this category should appear in when rendered on the templates.')class Meta:verbose_name='menu category'verbose_name_plural='menu categories'ordering = ['order', 'name']def __unicode__(self):return self.nameclass MenuItem(models.Model):CLASSIFICATION_CHOICES = (('neither', 'Neither'),('vegan', 'Vegan'),('vegetarian', 'Vegetarian'),)name = models.CharField(max_length=48, help_text='Name of the item on the menu.')description = models.CharField(max_length=128, null=True, blank=True, help_text='The description is a simple text description of the menu item.')category = models.ManyToManyField(MenuCategory, verbose_name='menu category', help_text='Category is the menu category that this menu item belongs to, i.e. \'Appetizers\'.')order = models.IntegerField(default=0, verbose_name='order', help_text='The order is to specify the order in which items show up on the menu.')price = models.IntegerField(help_text='The price is the cost of the item.')image = models.ImageField(upload_to='menu', null=True, blank=True, verbose_name='image', help_text='The image is an optional field that is associated with each menu item.')classification = models.CharField(max_length=10, choices=CLASSIFICATION_CHOICES, default=0, verbose_name='classification', help_text='Select if this item classifies as Vegetarian, Vegan, or Neither.')spicy = models.BooleanField(default=False, verbose_name='spicy?', help_text='Is this item spicy?')contains_peanuts = models.BooleanField(default=True, verbose_name='contain peanuts?', help_text='Does this item contain peanuts?')gluten_free = models.BooleanField(default=False, verbose_name='gluten free?', help_text='Is this item Gluten Free?')class Meta:verbose_name='menu item'verbose_name_plural='menu items'ordering = ['classification', 'order', 'name']def __unicode__(self):return self.name
views.py:
from .models import Menu,MenuCategory
from django.views.generic import ListView
class MenuView(ListView):
model= Menu
conext_object_name='name'
template_name = 'menu_list.html'
queryset = Menu.objects.all()
def get_context_data(self, **kwargs):
context = super(MenuView, self).get_context_data(**kwargs)
context['Menucategory'] = MenuCategory.objects.all()
context['Menus'] = self.queryset
return context
menu_list.html:
<div>
{%for item in Menu%}
<p>
{{item.menu}}
{{item.name}}
{{item.additiona_text}}
{{item.order}}
</p>
{%endfor%}
{%for item in MenuCategory%}
<p>
{{item.name}}
{{item.additiona_text}}
{{item.order}}
</p>
{%endfor%}
</div>
</body>
</html>
when i browse my localhost page after excuting runserver, it shows only
Django blog
Breakfast 1
Lunch 2
Dinner 3
but my desired output should be:
Breakfast
coffee
Items
1
2
3
Snacks
Items
1
2
3
Lunch
Starter
items
1
2
3
Main Courses
Items
1
2
3
how can i get this structured table using plain bootstrap table row column where in every row column i will query the items with for loop? is their any other way?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2bdd921a-d922-4fa0-84aa-e6a36042eb61%40googlegroups.com.
No comments:
Post a Comment