0

2つのテーブルがあります。1つはForeignKeyを使用しており、現在のサイトにはスラッグがあります。スラッグである参照を持つオブジェクトのセットを検索したい

すべての非営利団体は寄付を受け取ります。現在のラベルによって1つの非営利団体に寄付された寄付を表示するのを手伝ってください

これが私の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
4

1 に答える 1

1

これは私が考えた後のあなたです。

例:

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