0

I have 2 models in one app and 1 view. I'm currently pulling information from 1 model perfectly fine. However i wish to pull in another model from the same app and output them both to the same page.

The idea of the page is it being a a news hub so it's pulling through different types of news posts (from one model) and a different type of post which is from the other model.

I'm fairly new to Django so go easy! :) Anyway here is the code:

//VIEWS

def news_home(request):

    page_context = details(request, path="news-hub", only_context=True)

    recent_posts = NewsPost.objects.filter(live=True, case_study=False).order_by("-posted")[:5]

    recent_posts_pages = Paginator(recent_posts, 100)

    current_page = request.GET.get("page", 1)

    this_page = recent_posts_pages.page(current_page)

    notes = BriefingNote.objects.filter(live=True).order_by("-posted")

    news_categories = NewsCategory.objects.all()




    news_context = {
        "recent_posts": this_page.object_list,
        "news_categories": news_categories,
        "pages": recent_posts_pages,
        "note": notes,


    }

    context = dict(page_context)
    context.update(news_context)


    return render_to_response('news_hub_REDESIGN.html', context,  context_instance=RequestContext(request))

//model 1

class BriefingNote(models.Model):
    title = models.CharField(max_length=300)
    thumbnail = models.ImageField(upload_to='images/briefing_notes', blank=True)
    file = models.FileField(upload_to='files/briefing_notes')
    live = models.BooleanField(help_text="The post will only show on the frontend if the 'live' box is checked")
    categories = models.ManyToManyField("NewsCategory")

    # Dates
    posted = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return u"%s" % self.title

// model 2

class NewsPost(models.Model):
    title = models.CharField(max_length=400)
    slug = models.SlugField(help_text="This will form the URL of the post")

    summary = models.TextField(help_text="To be used on the listings pages. Any formatting here will be ignored on the listings page.")
    post = models.TextField(blank=True)
    #TO BE REMOVED????
    thumbnail = models.ImageField(help_text="To be displayed on listings pages", upload_to="images/news", blank=True)
    remove_thumbnail = models.BooleanField()

I'm outputting the content on the front end like so:

{% for post in recent_posts %}





                    <div class='news_first'>

                        <img class="news_thumb" src="/media/{% if post.article_type %}{{post.article_type.image}}{% endif %}{% if post.news_type %}{{post.news_type.image}}{% endif%}" alt="">
                        <h3><a href='{{post.get_absolute_url}}'>{% if post.article_type.title %}{{post.title}}{% endif %} <span>{{post.posted|date:"d/m/y"}}</span></a></h3>
                        <p class='news_summary'>
                            {% if post.thumbnail %}<a href='{{post.get_absolute_url}}'><img src='{% thumbnail post.thumbnail 120x100 crop upscale %}' alt='{{post.title}}' class='news_thumbnail'/></a>{% endif %}{{post.summary|striptags}} <a href='{{post.get_absolute_url}}'>Read full story &raquo;</a>
                        </p>
                        <div class='clearboth'></div>
                    </div>




            {% endfor %}

I was thinking perhaps i could output them both within the same forloop however they need to ordered by -posted. So i though this could mess things up.

If you need anymore info please let me know.

Thanks in advance.

5
  • duplicate: stackoverflow.com/questions/313137/… Commented Jul 13, 2012 at 11:50
  • Is this actually what I need to do? As that post they go off topic and it seems like they are doing something different at the end result? Commented Jul 13, 2012 at 11:51
  • I don't see a DateTimeField in NewsPost, how do you intend to order them by post date ? Commented Jul 13, 2012 at 11:56
  • I've not posted all the model fiels as the model is fairly large. Commented Jul 13, 2012 at 12:04
  • That post is unable to help? Can anyone help me out? It's driving me mad! Commented Jul 16, 2012 at 8:50

1 Answer 1

1

I've now solved the problem.

news_hub = list(chain(notes, recent_posts))

news_hub = sorted(
    chain(notes, recent_posts),
    key = attrgetter('posted'), reverse=True)[:10]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.