Model.objects.filter(蛞蝓=蛞蝓)

时间:2012-09-21 01:31:12

标签: django forms models

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

每个非营利组织都会获得捐款,帮助我显示当前标签给予一个非营利组织的捐款

这是我的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 个答案:

没有答案