Re: Problem with passing information from related model to a view

2009-06-25 Thread paul_tanner

Thx. DR.

Duh.  I had the foreignkey in an earlier test and then took it out
again.  Interesting that no error raised.
The other thing is that the template needed to call for the related
field thus:

 {{ obj.fname }} {{ obj.sname }} :
{{ obj.organisation.organisation }} (seems logical now)

With these 2 changes it works fine.
BTW. The .select_related() in the above is a red herring - not needed
for this purpose but I will doubtless learn its proper uses.
As to the tutorial I did read it.  Will look again to see why I missed
that template syntax detail.
I understand why the template process is silent in relation to non-
existent data fields.  However, with debug on this might not be the
ideal policy.

Paul

On 24 June, 18:44, Daniel Roseman  wrote:
> On Jun 24, 6:26 pm, paultanner  wrote:
>
>
>
>
>
> > As you can see from this I am just learning django.
> > I am trying to list contacts and their related organisations.  This
> > does list the contact names.
> > No errors are raised but fields from Organisations do not show with
> > this view and template.
> > I guess I need to be more explicit in referncing the related fields
> > but have not found out how, despite looking through tutorials.
> > Any help appreciated.  Paul
>
> > models.py
>
> > from django.db import models
>
> > class Contact(models.Model):
> >   fname = models.CharField(max_length=40)
> >   sname = models.CharField(max_length=40)
> >   email = models.CharField(max_length=60)
> >   dir_tel = models.CharField(max_length=25,blank=True)
> >   mobile = models.CharField(max_length=25,blank=True)
> >   blog = models.URLField(max_length=80,blank=True)
> >   linkedin = models.CharField(max_length=40,blank=True)
> >   def __str__(self):
> >     return '%s %s' %(self.fname,self.sname)
>
> > class Organisation(models.Model):
> >   organisation = models.CharField(max_length=60)
> >   addr1 = models.CharField(max_length=40,blank=True)
> >   addr2 = models.CharField(max_length=40,blank=True)
> >   city = models.CharField(max_length=40,blank=True)
> >   county = models.CharField(max_length=40,blank=True)
> >   pcode = models.CharField(max_length=12,blank=True)
> >   def __str__(self):
> >     return '%s' %(self.organisation)
>
> > views.py
>
> > from models import Contact
>
> > def contact_list(request):
> >    # where: .filter(fn="value")
> >    contact_list=list(Contact.objects.all().select_related())
> >    return render_to_response('contact_list.html',{
> >      'contact_list': contact_list,
> >    })
>
> > contact_list.html
>
> > {% block content %}
> > 
> >   {% for obj in contact_list %}
> >   
> >     {{ obj.fname }} {{ obj.sname }} :  {{ obj.organisation }}
> >   
> >   {% endfor %}
> > 
> > {% endblock %}
>
> The models don't seem to be related in any way. You need a ForeignKey,
> a OneToOneField or a ManyToManyField from one to the other to identify
> which organisation(s) are related to which contact(s).
>
> The tutorial shows you how to do this with Polls and Choices, you
> should be able to extrapolate that for your use case.
> --
> DR.- Hide quoted text -
>
> - Show quoted text -
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Problem with passing information from related model to a view

2009-06-24 Thread Daniel Roseman

On Jun 24, 6:26 pm, paultanner  wrote:
> As you can see from this I am just learning django.
> I am trying to list contacts and their related organisations.  This
> does list the contact names.
> No errors are raised but fields from Organisations do not show with
> this view and template.
> I guess I need to be more explicit in referncing the related fields
> but have not found out how, despite looking through tutorials.
> Any help appreciated.  Paul
>
> models.py
>
> from django.db import models
>
> class Contact(models.Model):
>   fname = models.CharField(max_length=40)
>   sname = models.CharField(max_length=40)
>   email = models.CharField(max_length=60)
>   dir_tel = models.CharField(max_length=25,blank=True)
>   mobile = models.CharField(max_length=25,blank=True)
>   blog = models.URLField(max_length=80,blank=True)
>   linkedin = models.CharField(max_length=40,blank=True)
>   def __str__(self):
>     return '%s %s' %(self.fname,self.sname)
>
> class Organisation(models.Model):
>   organisation = models.CharField(max_length=60)
>   addr1 = models.CharField(max_length=40,blank=True)
>   addr2 = models.CharField(max_length=40,blank=True)
>   city = models.CharField(max_length=40,blank=True)
>   county = models.CharField(max_length=40,blank=True)
>   pcode = models.CharField(max_length=12,blank=True)
>   def __str__(self):
>     return '%s' %(self.organisation)
>
> views.py
>
> from models import Contact
>
> def contact_list(request):
>    # where: .filter(fn="value")
>    contact_list=list(Contact.objects.all().select_related())
>    return render_to_response('contact_list.html',{
>      'contact_list': contact_list,
>    })
>
> contact_list.html
>
> {% block content %}
> 
>   {% for obj in contact_list %}
>   
>     {{ obj.fname }} {{ obj.sname }} :  {{ obj.organisation }}
>   
>   {% endfor %}
> 
> {% endblock %}

The models don't seem to be related in any way. You need a ForeignKey,
a OneToOneField or a ManyToManyField from one to the other to identify
which organisation(s) are related to which contact(s).

The tutorial shows you how to do this with Polls and Choices, you
should be able to extrapolate that for your use case.
--
DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Problem with passing information from related model to a view

2009-06-24 Thread paultanner

As you can see from this I am just learning django.
I am trying to list contacts and their related organisations.  This
does list the contact names.
No errors are raised but fields from Organisations do not show with
this view and template.
I guess I need to be more explicit in referncing the related fields
but have not found out how, despite looking through tutorials.
Any help appreciated.  Paul

models.py

from django.db import models

class Contact(models.Model):
  fname = models.CharField(max_length=40)
  sname = models.CharField(max_length=40)
  email = models.CharField(max_length=60)
  dir_tel = models.CharField(max_length=25,blank=True)
  mobile = models.CharField(max_length=25,blank=True)
  blog = models.URLField(max_length=80,blank=True)
  linkedin = models.CharField(max_length=40,blank=True)
  def __str__(self):
return '%s %s' %(self.fname,self.sname)

class Organisation(models.Model):
  organisation = models.CharField(max_length=60)
  addr1 = models.CharField(max_length=40,blank=True)
  addr2 = models.CharField(max_length=40,blank=True)
  city = models.CharField(max_length=40,blank=True)
  county = models.CharField(max_length=40,blank=True)
  pcode = models.CharField(max_length=12,blank=True)
  def __str__(self):
return '%s' %(self.organisation)

views.py

from models import Contact

def contact_list(request):
   # where: .filter(fn="value")
   contact_list=list(Contact.objects.all().select_related())
   return render_to_response('contact_list.html',{
 'contact_list': contact_list,
   })


contact_list.html

{% block content %}

  {% for obj in contact_list %}
  
{{ obj.fname }} {{ obj.sname }} :  {{ obj.organisation }}
  
  {% endfor %}

{% endblock %}

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---