0

我有 2 个表,其中一个带有 ForeignKey 当前站点有一个 slug 我想查找一组对象,这些对象的引用是 slug

每个非营利组织都会收到捐款,请帮我展示当前标签上给一个非营利组织的捐款

这是我的意见.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))

我的模型.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
4

1 回答 1

1

就是我想的你。

例子:

np = get_object_or_404(NonProfit,slug=slug)
related_donations = np.donation_set.all() 
于 2012-09-21T03:16:53.860 回答