Django - 获取没有任何相关对象的对象

时间:2015-02-19 18:46:55

标签: python mysql django

这可能是重复的。

所以我有:

class User(models.Model):
    school = models.ForeignKey(School)
    email = models.EmailField(max_length=254)
    password = models.CharField(max_length=128)
    firstname = models.CharField(max_length=50)
    surname = models.CharField(max_length=50)
    middlenames = models.CharField(max_length=100, blank=True)
    utype = models.CharField(max_length=1, choices=(('a','Administrator'),('t','Staff'),('s','Student')))
    lastlogin = models.DateTimeField(null=True)
    active = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)

class Staff(User):
    # Some methods

class Student(User):
    form = models.CharField(max_length=20)
    parentemail = models.EmailField(max_length=254)
    parentphone = models.CharField(max_length=20)

class Placement(models.Model):
    student = models.ForeignKey(Student)
    email = models.EmailField(max_length=254)
    name = models.CharField(max_length=100)
    company = models.CharField(max_length=100)
    position = models.CharField(max_length=50)
    house = models.CharField(max_length=50)
    street = models.CharField(max_length=50)
    town = models.CharField(max_length=50)
    county = models.CharField(max_length=50)
    postcode = models.CharField(max_length=8)
    phone = models.CharField(max_length=20)
    length = models.IntegerField(null=True)
    category = models.CharField(max_length=50)
    date = models.DateField(null=True)
    state = models.CharField(max_length=1, choices=(('A','In progress'),('B','Confirmed'),('C','Completed')))
    created = models.DateTimeField(auto_now_add=True)

class Visit(models.Model):
    placement = models.ForeignKey(Placement)
    staff = models.ForeignKey(Staff)
    date = models.DateField(null=True)
    feedback = models.CharField(max_length=1000)
    confirmed = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

我想检索所有没有任何访问权的展示位置(学生属于特定的学校)。

我的观点

def placements(request):
    if request.session['utype'] == 't':
    context['user'] = Staff.objects.get(pk=request.session['user'])
    if request.method == 'POST':
        placements = context['placements'] = Placement.objects.filter(student__school=school)
        for choice in visits:
            placement = Placement.objects.get(pk=choice)
            visit = placement.NewVisit(user, placement.date, '', False)
            visit.save()
        return redirect('workxp:visits')
    else:
        context['placements'] = Placement.objects.filter(student__school=school, visit_set=None)
    return render(request, 'workxp/staff/placements.html', context)

这是我到目前为止看到的其他问题,但它似乎没有用......

no_visits = Placement.objects.filter(student__school=school_object, visit_set=None)

如果我在访问中为展示位置字段提供相关名称,则可以使用。为什么不访问_set?

1 个答案:

答案 0 :(得分:4)

您需要使用visit_set来引用反向关系,因为它是一对多的

no_visits = Placement.objects.filter(student__school=school_object, visit_set=None) 

或者如果你要指定一个很好的相关名字......

class Visit(models.Model):
    placement = models.ForeignKey(Placement, related_name='visits')
    ...

no_visits = Placement.objects.filter(student__school=school_object, visits=None)