English 中文(简体)
什么是最有效的视图和模板?
原标题:What is the most effective view and template?

我有以下模式:

from django.db import models
import datetime

class Club(models.Model):
    establishment = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    def __unicode__(self):
        return self.establishment

class Day(models.Model):
    club = models.ForeignKey(Club)
    day = models.DateField( day )
    def __unicode__(self):
        return unicode(self.day)

class Court(models.Model):
    club = models.ForeignKey(Club)
    day = models.ForeignKey(Day)
    court = models.IntegerField(max_length=200)
    def __unicode__(self):
        return unicode(self.court)

class Slot(models.Model):
    club = models.ForeignKey(Club)
    day = models.ForeignKey(Day)
    court = models.ForeignKey(Court)
    slot = models.TimeField( slot )
    reservation = models.CharField(max_length=200)
    def __unicode__(self):
        return unicode(self.slot)

在空格模型中, 每一个“ 绘图” 可以是“ 打开”, 也可以是“ 保留” 字段中的任何其他值( 通常是电子邮件 ) 。 我想做的是: 给一个已传递到函数的俱乐部_ id, 在“ 点” 中显示所有字段, 其保留值为“ 打开” 和“ 日”, 与今天相等... 然后将它传递到模板中 。

def avail_times(request, club_id):
    p = get_object_or_404(Slot,pk=club_id)
    return render_to_response( reserve/templates/avail_times.html , { times :p})

我无法用我拥有的当前视图/ 模板来做到这一点; 它只返回一个时间 。 我如何在视图中引用“ day” 和“ club” (给 Club_ id), 然后在模板中显示上述内容?

最佳回答

首先,该查询不按你说的做。要获得今天所有为某个俱乐部开放的空位,你需要这样的东西:

open_slots = Slot.objects.filter(club_id=club_id, day__day=datetime.date.today(),
                                 reservation= open )

现在,在您的模板中,您可以通过 open_slots 进行循环,并显示 slot 时间(您应该在这里选择一个更好的字段名称) 。

顺便说一下,我不明白 < code> Day < Day 模式的要点,或者至少你为什么从法院到日都有FK。法院是同一天一样的,而唯一应该与Day有关的东西是Slot。同样,Day不需要FK到Club(它仍然是星期一),事实上,你可以完全删除Day模式,直接在Slot上使用 Date Frielt。

问题回答

您应该使用< a href=>"https://docs.djangoproject.com/en/dev/ref/models/querysets/ #django.db.models.query.QuerySet.select_connects. rel=“nofollow”>select_contal () 来检索相关对象(aka: 外国Keys)

在您看来, 引用它时, 只需输入类型 :

club = p.club

这将为您提供相关的俱乐部对象( 在 db 中, 如果您不使用特定相关对象, 则在 db 中再次点击) 。

您已经把全部信息存储在 Slot 对象 (p) 中, 其唯一“ 真实” 成员是空格字段( 正如您指出的, 您正在看到的日期字段) 。





相关问题
Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

How to create a submit button template in Oracle APEX?

I m trying to create a template for a button in Oracle APEX but I don t seem to have access to the appropriate substitution strings to make it work. For non-templated buttons APEX seems to insert a ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Load ruby on rails template from database

I wonder if there is a way for me to store ruby on rails view files into database store and have that fetched directly from there. The reason is I want to create a CMS with all the user data stored in ...

Templated HTML Editor

I m looking for a HTML editor that kinda supports templated editing or live snippets or something like that. Background: I m working on a website for a friend. As there are no specifications what the ...

Dreamweaver changing path to site s reference instead of local

I have noticed recently, when I apply a template to a new HTML website, all the relative paths are pointed to my local files, example: file:///C|/webstuff/files but I cannot set them to relative paths ...

WPF ListView : Header styling

I want to have a ListView with columns and a particular style: The background for ALL column headers should be transparent except when the mouse is over in one of them. When this happends, the ...

热门标签