Comparing two lists
Hello, I've only just started with Django and need a bit of help steering myself in the right direction. I want to create a page that has a list of possible forms that could be filled in for a patient in a medical study and differentiate between those forms which have already been completed and those that have yet to be started. I have 3 tables: 'Building' which contains the list of forms, 'Street' which contains the list of patients and 'Address' which records which form exists for each patient. The relevant bit of models.py looks like this: class Building(models.Model): crf_code = models.CharField(max_length=6, primary_key=True) crf_name = models.CharField(max_length=100) class Street(models.Model): village = models.ForeignKey(Village) patient_number = models.PositiveIntegerField() initials = models.CharField(max_length=3) class Address(models.Model): street = models.ForeignKey(Street, related_name='addresses') building = models.ForeignKey(Building, related_name='addresses') completed = models.BooleanField(default=False) I've created a view that creates two lists, one of all buildings (i.e. forms that could be entered for a patient) and another of forms already existing, filtered by patient id: def street2(request, patientid): context_dict = {} try: address = Address.objects.filter(street__pk=patientid) crf = Building.objects.all() context_dict['address'] = address context_dict['crf'] = crf except Street.DoesNotExist: context_dict['address'] = None context_dict['crf'] = None return render(request, 'trial/street2.html', context_dict) My problem comes with relating the two lists to each other in my template. what I want to do is list the buildings and then, depending if the form already exists, display an "edit form" or "create form" link: {% for x in address %} {{ x.building }} {% if %} link to edit form {% elif %} link to create form {% endif %} {% endfor %} I'm not sure if I've explained myself clearly; if not I apologize. I'm only just starting out and am floundering a bit. Any help or guidance would be hugely appreciated. Thank you, Richard -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/237bb16a-d76e-42b9-bd46-a7b2f9c43f4c%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Comparing two lists
Thanks for replying to my question. What I am trying to do is for each building in the list see if there is a corresponding address record for that building. Thus I'm trying to test the opposite of what you've pointed out i.e. not that there will be an address without a building, but if that building has an address. On Thursday, 26 January 2017 19:21:18 UTC+11, ludovic coues wrote: > > You are assuming you can have address without building. > {% if x.building %} > > 2017-01-26 0:27 GMT+01:00 Richard Hall >: > > > Hello, I've only just started with Django and need a bit of help > steering > > myself in the right direction. > > > > I want to create a page that has a list of possible forms that could be > > filled in for a patient in a medical study and differentiate between > those > > forms which have already been completed and those that have yet to be > > started. I have 3 tables: 'Building' which contains the list of forms, > > 'Street' which contains the list of patients and 'Address' which records > > which form exists for each patient. The relevant bit of models.py looks > like > > this: > > > > class Building(models.Model): > > crf_code = models.CharField(max_length=6, primary_key=True) > > crf_name = models.CharField(max_length=100) > > > > class Street(models.Model): > > village = models.ForeignKey(Village) > > patient_number = models.PositiveIntegerField() > > initials = models.CharField(max_length=3) > > > > > > class Address(models.Model): > > street = models.ForeignKey(Street, related_name='addresses') > > building = models.ForeignKey(Building, related_name='addresses') > > completed = models.BooleanField(default=False) > > > > I've created a view that creates two lists, one of all buildings (i.e. > forms > > that could be entered for a patient) and another of forms already > existing, > > filtered by patient id: > > > > def street2(request, patientid): > > context_dict = {} > > try: > > address = Address.objects.filter(street__pk=patientid) > > crf = Building.objects.all() > > context_dict['address'] = address > > context_dict['crf'] = crf > > except Street.DoesNotExist: > > context_dict['address'] = None > > context_dict['crf'] = None > > > > return render(request, 'trial/street2.html', context_dict) > > > > My problem comes with relating the two lists to each other in my > template. > > what I want to do is list the buildings and then, depending if the form > > already exists, display an "edit form" or "create form" link: > > > > > > > > {% for x in address %} > > > > {{ x.building }} > > > > {% if %} > > link to edit form > > {% elif %} > > link to create form > > {% endif %} > > > > > > {% endfor %} > > > > > > > > I'm not sure if I've explained myself clearly; if not I apologize. I'm > only > > just starting out and am floundering a bit. Any help or guidance would > be > > hugely appreciated. > > > > Thank you, > > > > Richard > > > > -- > > You received this message because you are subscribed to the Google > Groups > > "Django users" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to django-users...@googlegroups.com . > > To post to this group, send email to django...@googlegroups.com > . > > Visit this group at https://groups.google.com/group/django-users. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/django-users/237bb16a-d76e-42b9-bd46-a7b2f9c43f4c%40googlegroups.com. > > > > For more options, visit https://groups.google.com/d/optout. > > > > -- > > Cordialement, Coues Ludovic > +336 148 743 42 > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0ea9674b-caef-479a-a51e-3b6e544615d7%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Comparing two lists
Thanks for replying, I need all the help I can get. What I'm trying to do is the opposite of what I seem to be doing. Rather than looking for an address without a building I am trying to see, from my list of all buildings, which one has an address (filtered by patientid). I can easily list all addresses associated with a building, it is just getting only the ones for a particular patient that I'm stuck with. Thanks, Richard -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0ec5c059-a0b3-41ab-8f42-2603e0d640e5%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Comparing two lists
Melvyn, thanks for looking at this. Despite my inept attempt at explanation you've understood what I am trying to do - I need to revisit my relationships and fully explore my options at the model level. Thanks again, Richard P.S. I'll try to use clearer analogies in the future - in my head the database is a village which is really obvious, but only if you are in my head! -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9f132096-e397-42fd-8d8e-31f7c64a2bfa%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.