Django newbie here...
I have three models which I'm currently wrestling with and I obtained a desired solution. However, the solution is certainly not efficient nor good practice. I just don't know enough to adequately implement what I want.
Here are the models...
basics/models.py
class Tag(models.Model):
slug = models.SlugField(max_length=200, unique=True)
def __unicode__(self):
return self.slug
def get_absolute_url(self):
return reverse("tag_index", kwargs={"slug": self.slug})
class post(models.Model):
title = models.CharField(max_length = 180)
// a bunch of other fields here
tags = models.ManyToManyField(Tag)
def get_absolute_url(self):
return reverse("post_detail", kwargs={"slug": self.slug})
def __unicode__(self):
return self.title
class Meta:
verbose_name = "Blog Post"
verbose_name_plural = "Blog Posts"
ordering = ["-created"]
projects/models.py
class project(models.Model):
projectTitle = models.CharField(max_length=150, null=False)
projectTag = models.OneToOneField(Tag, primary_key=True)
description = MarkdownField()
def __unicode__(self): # __unicode__ on Python 2
return self.projectTitle
class Meta:
verbose_name = "Project"
verbose_name_plural = "Projects"
Now... what I would like to do is to create an adequate view, which passes to my template only the posts which are tagged with the project tags rather than all posts as it is currently doing.
projects/views.py
class projectFeed(generic.ListView):
queryset = project.objects.all()
template_name = "projects.html"
paginate_by = 5
def get_context_data(self, **kwargs):
context = super(projectFeed, self).get_context_data(**kwargs)
# slug = self.kwargs['projectTag']
# tag = Tag.objects.get(slug=slug)
context['posts'] = post.objects.all()#filter(tags=tag)
return context
As you can see, I tried some stuff that I used for creating the view for displaying all posts with a certain tag but I couldn't get it to work here.
projects/templates/projects.html
{% for project in project_list %}
<div class='postContent'>
<!-- project stuff here -->
{% for post in posts %}
{% if project.projectTag in post.tags.all %}
<p><a href="{% url 'post_detail' slug=post.slug %}"> {{ post.title }} </a></p>
{% endif %}
{% endfor %}
</div>
{% endfor %}
Ideally... I want to pass a nested list of sorts where each project in the project_list has an associated list of posts which I can iterate over in the inner for loop without needing to check the tags. That is, I want to filter and arrange the data in the view method/class OR another more efficient place.