I am creating a django app where the admin can add travel packages. To add the explicit details of the package I created a child that will handle those values. Here is how the code looks like.
class Category(models.Model):
title = models.CharField(max_length=30)
slug = models.SlugField(unique=True)
class Meta:
ordering = ['title']
def __str__(self):
return self.title
class SafariPackages(models.Model):
image = models.ImageField(upload_to='SafariPackages/%Y/%m/%d', null=False)
title = models.CharField(max_length=200)
cost = models.FloatField(null=False)
days = models.FloatField(blank=False, null=False)
nights = models.FloatField(blank=False, null=False)
category = models.ManyToManyField(Category)
exodus = models.CharField(max_length=200)
destination = models.CharField(max_length=200)
telegram = models.TextField(max_length=140, null=False)
description = models.TextField(null=False)
slug = models.SlugField(max_length=200, unique=True)
objects = EntryQuerySet.as_manager()
def __str__(self):
return self.title
class Meta:
verbose_name = 'Safari Package Entry'
verbose_name_plural = 'Safari Package Entries'
class DayNumber(models.Model):
daydetails = models.ForeignKey(SafariPackages, related_name='daydetails', default=1, on_delete=models.CASCADE)
day_number = models.CharField( max_length=10, null=True, blank=True)
image = models.ImageField (upload_to='SafariPackagesDetails/%Y/%m/%d', null=False, default=1)
day_description = models.TextField()
The views that I have so far look like this
def safaripackages_view(request, *args,**kwargs):
print (args, kwargs)
print (request.user)
all_safaripackages = SafariPackages.objects.all()
#Paginator
paginator = Paginator(all_safaripackages, 8)
page = request.GET.get('page')
try:
all_safaripackages = paginator.page(page)
except PageNotAnInteger:
all_safaripackages = paginator.page(1)
except EmptyPage:
all_safaripackages = paginator.page(paginator.num_pages)
context = {
'all_safaripackages': all_safaripackages,
}
return render (request, "safaripackages.html", context)
def single_safaripackage(request, post_id):
safaripackage = SafariPackages.objects.get(pk=post_id)
return render (request, 'single_safaripackage.html', {'safaripackage':safaripackage,})
How do I include the values of the DayNumber model in the single_safaripackage view?
2
Answers
In accordance to my understanding,you are trying to render the day numbers of the packages from the child model.
The trick is:
“the parent model”.”the child model”_set.all()
eg: parent.child_set.all()
*the child model to be lower case all through regardless of its original form in the model.py
Here is the solution to dynamically render a specific package to the browser:
Here is the solution to render all packages:
In your template you can have like a table to render