This may be a duplicate.
So I have:
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)
And I want to retrieve all Placements which do not have any Visits (and the Student belongs to a particular School).
My views
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)
This is what I have so far from looking at other questions, but it doesn't seem to work...
no_visits = Placement.objects.filter(student__school=school_object, visit_set=None)
If I give a related name to the placement field in Visit, it works. Why not visit_set?
You need to use visit_set
to reference the reverse relation since it's one to many
no_visits = Placement.objects.filter(student__school=school_object, visit_set=None)
or if you were to specify a nice related name...
class Visit(models.Model):
placement = models.ForeignKey(Placement, related_name='visits')
...
no_visits = Placement.objects.filter(student__school=school_object, visits=None)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With