I'm building a shopping cart. In my shopping cart an item can be composed of other items. I need to display a set of items with their corresponding associated parts in a single template. I know how to show a single item with its corresponding parts in a template, but I can't seem to figure out how to show more than one item, each with its own list of included parts.
I have fiddled with every permutation of tags in the template file:
# checkout.html
{% for item in cart_items %}
<tr>
<td class="left">
{{ item.name }}
<ul>
{% for part in item.product.buildpart.part_set.all %}
<li>{{ part.name }}
{% endfor %}
</ul>
</td>
<td>${{ item.price }}</td>
<td>{{ item.quantity }}</td>
<td class="right">${{ item.lineItemTotal }}</td>
</tr>
{% endfor %}
Here is the vew that generates the template:
# views.py
def checkout(request):
cart_items = get_cart_items(request)
<snip>
return render(request, 'checkout.html', locals())
And here's the get_cart_items() function that returns all the items in the user's shopping cart:
# cart.py
def get_cart_items(request):
""" return all items from the current user's cart """
return CartItem.objects.filter(cart_id=get_cart_id(request))
Here's the CartItem model:
# models.py
class Item(models.Model):
cart_id = models.CharField(max_length=50)
quantity = models.IntegerField(default=1)
product = models.ForeignKey(PartModel, unique=False)
class Meta:
abstract = True
<snip>
class CartItem(Item):
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['date_added']
verbose_name = "Cart Item"
<snip>
The 'product' field is a ForeignKey to the PartModel model:
# models.py
class PartModel(models.Model):
family = models.ForeignKey(PartFamily)
name = models.CharField("Model Name", max_length=50, unique=True)
slug = models.SlugField(help_text="http://www.Knowele.com/<b>*slug*</b>",
unique=True)
<snip>
buildpart = models.ManyToManyField('self', through='BuildPart',
symmetrical=False, related_name='+')
class Meta:
ordering = ['name']
verbose_name = "Product Model"
<snip>
The PartModel model has a ManyToMany relationship with itself through the buildpart field and the BuildPart model to facilitate the notion of catalog items that can be composed of other catalog items:
# models.py
class Build(models.Model):
build = models.ForeignKey(PartModel, related_name='+')
part = models.ForeignKey(PartModel, related_name='+')
quantity = models.PositiveSmallIntegerField(default=1)
class Meta:
abstract = True
unique_together = ('build', 'part')
def __unicode__(self):
return self.build.name + ' with ' + str(self.quantity) + ' * ' + \
self.part.family.make.name + ' ' + self.part.name
class BuildPart(Build):
pass
class Meta:
verbose_name = "Build Part"
I can't seem to make the necessary ForeignKey traversals in the template (listed above) in order to get all the parts associated with the user's items in the CartItem model. Is it something I'm not doing right in the template or am I not packaging up the right QuerySets in my view?
The second part of this issue is that once I get those parts, I need them to show up in the order specified in the 'order' integer field of the PartType model:
# models.py
class PartType(models.Model):
name = models.CharField("Part Type", max_length=30, unique=True)
slug = models.SlugField(unique=True)
order = models.PositiveSmallIntegerField()
description = models.TextField(blank=True, null=True)
class Meta:
ordering = ['name']
verbose_name = "Product Type"
def __unicode__(self):
return self.name
class PartFamily(models.Model):
make = models.ForeignKey(PartMake)
type = models.ForeignKey(PartType)
name = models.CharField("Family Name", max_length=30,
unique=True)
slug = models.SlugField(unique=True)
url = models.URLField("URL", blank=True, null=True)
description = models.TextField(blank=True, null=True)
class Meta:
ordering = ['name']
verbose_name = "Product Family"
verbose_name_plural = "Product Families"
def __unicode__(self):
return self.name
So as you can see, in the PartModel model, the 'family' field is a ForeignKey to the PartFamily model, and in the PartFamily model the 'type' field is a ForeignKey to the PartType model, within which is the all-important 'order' field that the parts need to be ordered by.
I hope this makes sense and you can see why this is so complicated for a noob like me.
Just iterate on item.product.buildpart.all:
{% for item in cart_items %}
[...]
{% for part in item.product.buildpart.all %}
{{ part.name }}[...]
{% endfor %}
{% endfor %}
Related
I have an app for a shopping list, and I want to display values from my ingredients ManyToManyField but what I am getting instead is the name of the recipe that I created.
Could you please advise me on how to correctly do it?
models.py
class Ingredients(models.Model):
name=models.CharField(max_length=100, default='-', blank=True, unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'składnik'
verbose_name_plural = 'składniki'
def __str__(self):
return str(self.name)
class Category(models.Model):
name=models.CharField(max_length=250, default='-', blank=True, unique=True)
slug = models.SlugField(unique=True, blank=True)
class Meta:
ordering = ('name',)
verbose_name = 'kategoria'
verbose_name_plural = 'kategorie'
def get_absolute_url(self):
return reverse("kategoria", kwargs={"slug": self.slug})
def __str__(self):
return str(self.name)
class Recipe(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, default='-')
name = models.CharField(max_length=400, blank=False, unique=False)
body = models.TextField(blank=True, default='')
ingredients = models.ManyToManyField(Ingredients)
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True)
date_created = models.DateTimeField('Utworzono', default=timezone.now)
last_updated = models.DateTimeField('Ostatnia zmiana',auto_now=True)
when_to_eat = models.DateField('Kalendarz', default=timezone.now)
tags = TaggableManager()
slug = models.SlugField(unique=True, blank=True)
class Meta:
ordering = ('name',)
verbose_name = 'przepis'
verbose_name_plural = 'przepisy'
def get_absolute_url(self):
return reverse("przepis", kwargs={"slug": self.slug})
def __str__(self):
return str(self.name)
views.py
class RecipeListView(ListView):
model = Recipe
template_name ='recipe_list.html'
queryset = Recipe.objects.all()
urls.py
urlpatterns = [
path('przepisy/', views.RecipeListView.as_view(), name='recipe_list'),
path('przepis/<slug:slug>/', views.RecipeDetailView.as_view(), name='przepis'),
]
recipe_list.html
<p class="card-text">Składniki:<br>
{% for ingredient in object_list %}
<li>{{ingredient.name}}</li>
{% endfor %}
</p>
you should try something like this:
{% for ingredients in object_list %}
{% for ingredient in ingredients.name.all %}
<li>{{ingredient.name}}</li>
{% endfor %}
{% endfor %}
Try this:
<p class="card-text">Składniki:<br>
{% for recipe in object_list %}
{% for ingredient in recipe.ingredients.all %}
<li>{{ingredient.name}}</li>
{% endfor %}
{% endfor %}
</p>
As you want to get all name from your Ingredients model but you are using {{ingredient.name}} which will show you all name from your Recipe Model. You need to be write {{ingredient.ingredients.name}} So the code will be looke like this
<p class="card-text">Składniki:<br>
{% for ingredient in object_list %}
<li>{{ingredient.ingredients.name}}</li>
{% endfor %}
</p>
As you are using Recipe model in your ListView so when you are writing {{ingredient.name}} it's similar to like this Recipe Model > name when we are writing {{ingredient.ingredients.name}} it's similar to Recipe Model > ManyToManyField of ingredients > garbing name from Ingredients model
I am building menu items for Irvine class and want to categorize them by Category
models.py
class Irvine(models.Model):
objects = None
name = models.CharField(max_length=50, verbose_name='Irvine Item')
description = models.TextField(null=True, blank=True)
size = models.FloatField(null=True, blank=True)
price = models.FloatField(null=True, blank=True)
published = models.DateTimeField(auto_now_add=True, db_index=True)
category = models.ForeignKey('Category', null=True, on_delete=models.PROTECT, verbose_name='Category')
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'Irvine'
verbose_name = 'Irvine Item'
ordering = ['-published']
class Category(models.Model):
objects = None
name = models.CharField(max_length=30, db_index=True, verbose_name="Category")
published = models.DateTimeField(auto_now_add=True, db_index=True)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = '* Categories'
verbose_name = 'Category'
ordering = ['name']
view.py
def irvine(request):
irvine = Irvine.objects.all()
context = {'irvine': irvine}
return render(request, 'cafe/irvine.html', context)
def by_category(request, category_id):
santaanna = SantaAnna.objects.filter(category=category_id)
costamesa = CostaMesa.objects.filter(category=category_id)
irvine = Irvine.objects.filter(category=category_id)
categories = Category.objects.all()
current_category = Category.objects.get(pk=category_id)
context = {'santaanna': santaanna, 'categories': categories, 'costamesa': costamesa, 'irvine': irvine, 'current_category': current_category}
return render(request, 'cafe/by_category.html', context)
urls.py
urlpatterns = [
path('add/', ItemsCreateView.as_view(), name='add'),
path('<int:category_id>/', by_category, name='by_category'),
path('', index, name='index'),
path('irvine', irvine),
with
{% for i in irvine %}
{}
<tr class="danger">
<th scope="row" width="20%">{{ i.name }}</th>
<td width="60%">{{ i.description }}</td>
<td width="10%">{{ i.size }}</td>
<td width="10%">{{ i.price }}</td>
</tr>
{% endfor %}
I can grab all items from class Irvine, but how do i get items from this class by category
You can't directly check using i.category because it has list of values.
Try using i.category.name.
If you have serializer, please update the full code.
{% for i in irvine %} {% if i.category.name == 'Appetizers' %}, it will work
I want to fetch all the foreignkey table's attribute and show it in my HTML template. Here is my code in models, views and in the template:
models.py:
class OrderDashboard(models.Model):
title = models.CharField(max_length=100,default=None)
single_slug = models.SlugField(max_length=100, default=1)
description = models.TextField(max_length=1000)
thumb = models.ImageField()
date = models.DateField()
def __str__(self):
return self.title
class OrderScenario(models.Model):
webshop = models.CharField(max_length=100)
title = models.ForeignKey(OrderDashboard, default=None, on_delete=models.SET_DEFAULT)
order_qty = models.TextField(max_length=10)
order_date = models.DateField()
current_status = models.CharField(max_length=100)
ticket = models.CharField(max_length=200)
remark = models.TextField()
class Meta:
verbose_name_plural = "Scenario"
def __str__(self):
return self.webshop
Views.py:
def single_slug(request, single_slug):
report = OrderDashboard.objects.get(single_slug=single_slug)
return render(request, 'order_dashboard/report.html', {'report': report,
'OrderScenario': OrderScenario.objects.all})
I only want to view all the scenarios added in OrderScenario with respect to Title in OrderDashboard.
You should use backward relationship here; if you are passing the slug through the url, you can use:
views.py:
def single_slug(request, slug): # why you have self as the first argument?
report = OrderDashboard.objects.get(single_slug=slug)
return render(request, 'order_dashboard/report.html', {'report': report}
report.html:
{{ report.title }}
</p>Order Scenarios:</p>
{% for scenario in report.orderscenario_set.all %}
{{ scenario }}
{% endfor %}
I'm newbie in coding, and I'm trying my first Django app.
This time I have three different models, but with a single form at the momment when I hit Save I want to save data on that form model and in the others 2 models.
So in my display I want to loop the other two models to show what I have.
Is that even possible?
models.py
class Personame(models.Model):
name = models.CharField(max_length=128)
def __str__(self):
return self.name
class Personlastname(models.Model):
last_name = models.CharField(max_length=128)
def __str__(self):
return self.last_name
class Personinfo(models.Model):
personame = models.CharField(max_length=128)
personlastname = models.CharField(max_length=128)
address = models.TextField()
phone_number = models.CharField(max_length=128)
hobbies =models.CharField(max_length=128)
def __str__(self):
return self.personame
forms.py:
class personform(forms.ModelForm):
personame = forms.CharField(label = "Name")
personlastname = forms.CharField(label = "Last Name")
class Meta:
model = Personinfo
fields = ["personame","personlastname","address","phone_number","hobbies"]
views.py:
def index(request):
queryset = Personame.objects.all()
queryset2 = Personlastname.objects.all()
qs = chain(queryset,queryset2)
form = personform(request.POST or None)
context = {
"qs": qs,
"form":form,
}
if form.is_valid():
instance = form.save()
return render(request, "index.html", context)
index.html:
<form method="POST" action="">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save!" />
</form>
<table >
<tr>
<th>Name</th>
<th>Last Name</th>
</tr>
{% for item in qs %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.last_name }}</td>
</tr>
{% endfor %}
</table>
Basically I want to save name in Personinfo and Personame, and I want to save last_name in Personinfo and Personlastname. So I can show Personame and Personlastname in template.
Can anyone help me please?
I would be very happy for your help... Thanks
Well your models don't make any sense at all. They really should be one model. you have not defined any relationship between any of the models. To do that you will have to add a OneToOneField or a ForeignKey. That effectively means you are having an extra column for each item of data. Totally redundant.
class Personinfo(models.Model):
name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
address = models.TextField()
phone_number = models.CharField(max_length=128)
hobbies =models.CharField(max_length=128)
def __str__(self):
return self.personame
This makes your life so much easier with simpler coding.
your models don't have class Meta , need to define the table name in class meta like :
class Meta:
managed = True
db_table = 'tableName'
Then u need to override save function in personform
I've looked through the similar questions and was unable to find a solution that fits or I'm missing something? I have two models(SafetyCourse and SafetyCourseTaken) I have a foreign key relationship that points from "safety courses taken" to safety course, shown below:
models.py
class SafetyCourse(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=128, unique=True)
def __str__(self):
return self.name
class SafetyCoursesTaken(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
profile = models.ForeignKey(EmployeeProfile, on_delete=models.CASCADE)
course = models.ForeignKey(SafetyCourse, on_delete=models.CASCADE, related_name='course_name')
conducted_date = models.DateTimeField(null=True, blank=True)
expiration_date = models.DateTimeField(null=True, blank=True)
class Meta:
verbose_name_plural = 'Safety Courses Taken'
views.py
class ManageSafetyCourseTakenView(LoginRequiredMixin, generic.ListView):
login_url = reverse_lazy('users:login')
model = SafetyCoursesTaken
template_name = 'ppm/courses-taken.html'
paginate_by = 10
# override get_queryset to only show training related to employee profile
def get_queryset(self):
pk = self.kwargs['pk']
return SafetyCoursesTaken.objects.filter(profile_id=pk)
course-taken.html(template)
{% for course_taken in object_list %}
<tr>
<td>{{ course_taken.course_id}}</td>
</tr>
{% endfor %}
I've tried a number of solutions to similar questions but was unable to find a correct one. I've tried: course_taken.course_name_set.select_related, course_taken.course_name_set, and a few others. What I want to do is just display the name of the course instead of the course id. What am I doing wrong?
Looking at your schema, I think it should be this in the template:
{% for course_taken in object_list %}
<tr>
<td>{{ course_taken.course.name }}</td>
</tr>
{% endfor %}