2012-09-21 107 views
0

我有2代表一個與一個ForeignKey當前站點具有蛞蝓我想查找一組有一個參考是蛞蝓Model.objects.filter(蛞蝓=蛞蝓)

每非營利對象得到捐款,幫我給在標籤上本

這裏給一個非營利性的捐款是我views.py

def nonProfit(request,slug): 
    """ we need a page to serve info about non profit pages""" 
    np = get_object_or_404(NonProfit,slug=slug) 
    try: 
     nonProfit_slug = NonProfit.objects.filter(name=slug) 
     donation_list = Donation.objects.filter(towards=nonProfit_slug) 

     paginator = Paginator(donation_list, 4) # Show 4 contacts per page 

     page = request.GET.get('page') 
     try: 
      donations = paginator.page(page) 
     except PageNotAnInteger: 
      # If page is not an integer, deliver first page. 
      donations = paginator.page(1) 
     except EmptyPage: 
      # If page is out of range (e.g. 9999), deliver last page of results. 
      donations = paginator.page(paginator.num_pages) 
     return render_to_response('nonProfit.html', 
    {"donations":donations, 
    "site":settings.SITE_DOMAIN, 
    "nonProfit":np}, 
    context_instance=RequestContext(request)) 

    except: # no donations 
     return render_to_response('nonProfit.html',{'nonProfit':np},context_instance=RequestContext(request)) 

我的models.py

class Donation (models.Model): 
    user = models.ForeignKey(User) 
    name = models.CharField(max_length=50) 
    description = models.CharField(max_length=3000) 
    towards = models.ForeignKey(NonProfit) 
    image = models.ImageField(upload_to='photos/%Y/%m/%d') 
    slug = models.SlugField(max_length=128) 
    slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',)) 
    ticketPrice = models.IntegerField() 

    enable_comments = models.BooleanField() 
class NonProfit (models.Model): 
    user = models.ForeignKey(User) 
    name = models.CharField(max_length=50) 
    address = models.CharField(max_length=50) 
    city = models.CharField(max_length=60) 
    state_province = models.CharField(max_length=30) 
    country = models.CharField(max_length=50) 
    website = models.URLField() 
    email = models.EmailField() 
    money_gained = models.IntegerField() 
    money_gained.null = True 
    slug = models.SlugField(max_length=128) 
    slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',)) 


    description = models.CharField(max_length=3000) 

    def __unicode__(self): 
     return self.slug 

回答

0

This就是我想的。

例子:

np = get_object_or_404(NonProfit,slug=slug) 
related_donations = np.donation_set.all() 
+0

工作就像一個魅力,謝謝。我不知道_set.all()謝謝 – Klanestro