English 中文(简体)
List, Group multiple instances of an object based on ManyToMany field in Django
原标题:

Given the following models, I need to return a list of Links for each Place, grouped by Category.

class Place(models.Model):
    name = models.CharField(max_length=100)

class Category(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

class Link(models.Model):
    name = models.CharField(max_length=100)
    url = models.URLField()
    place = models.ManyToManyField( Place )
    categories = models.ManyToManyField( Category )
    description = models.TextField()
    featured = models.BooleanField()
    published = models.BooleanField()
    date_added = models.DateField(auto_now_add=True)

The result would look something like:

Some Place
Information about Some Place ... (not really important to question)

Banks
Bank of America
Commerce Bank
First National Bank

Financial Services
Bank of America
Edward Jones

Loans
Commerce Bank
First National Bank

As you can see, a Link should be listed in every Category in which it belongs.

I tried to loop through categories and concatenate the querysets, but with ManyToMany fields, it doesn t seem possible to do the sort necessary that can be passed into the template and grouped there.

问题回答

What is the problem? I tried this myself on the console, here is the traceback, that should help you.

 >>> ny = Place.objects.create(name= NewYork )
 >>> bank = Category.objects.create(name= bank ,description= aaa )
 >>> bofa = Link.objects.create(name= BofA ,url= http://google.com ,description= a )
 >>> bofa.place.add(ny)
 >>> bofa.categories.add(bank)
 >>> ny.link_set.all()
 [Link: Link object]    # This is `bofa`
 >>> ny.link_set.filter(categories=bank)
 [Link: Link object]
 >>> 

Where is the problem? To display all the links category wise, of a particular place,

  • Get the place object Use get_object_or_404. ny in the example above
  • Obtain all links in the place, filter for the category you need. (You may need to obtain list of all categories in this place, similarly.)




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签