Models and relationships

36 views
Skip to first unread message

Chris Strasser

unread,
Jul 6, 2015, 8:04:55 AM7/6/15
to django...@googlegroups.com
Hi ,I am fairly new to Django and am struggling with models hope someone can sort me out. 


Models:
class Organization(models.Model): #customer or vendor person or company orgid = models.AutoField(db_column='OrgID', primary_key=True) is_company = models.NullBooleanField(db_column='IsCompany', default =True) organizationname = models.CharField(db_column='OrgName', max_length=75,blank=True, null=True) main_phone = models.CharField(db_column='MainPhone',max_length=20, blank=True, null=True) ...
...
  class Location(models.Model): locationid = models.AutoField(db_column='LocationID', primary_key=True) orgid = models.ForeignKey(Organization, db_column='OrgID') locationname = models.CharField(db_column='LocationName', max_length=75, null=True) address1 = models.CharField(db_column='Address1', max_length=200, blank=True, null=True) ...
...

class Contact(models.Model): contactid = models.AutoField(db_column='ContactID', primary_key=True) locationid = models.ForeignKey(Location, db_column='LocationID') firstname = models.CharField(db_column='FirstName', max_length=50,blank=True, null=True) lastname = models.CharField(db_column='LastName', max_length=50,blank=True, null=True) ...
...

my problem is with reverse lookups (i think it is called) the docs say that i can access entries through blog.entry,(by lowercaseing the class)
it appears to me that i have the same setup with contact and location but i am getting errors ??


what I am trying to do is access a contacts address and organization at both the views level and the templates.(I would like to create a
list of contacts, their company and their address ordered by contact name)

if someone would point me in the right direction it would be greatly appreciated.

Thanks.

Sadaf Noor

unread,
Jul 6, 2015, 8:59:03 AM7/6/15
to django...@googlegroups.com
If you have at least one entry in contact with a valid location, then it should work: 
from app.models import Contact
contact = Contact.all()[0]
print contact.locationid.locationname

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8abd757c-cd28-4c2f-82c9-ecd8c60dff4d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
 Md. Sadaf Noor (@sadaf2605)
 www.sadafnoor.com

Rafael E. Ferrero

unread,
Jul 6, 2015, 9:01:43 AM7/6/15
to django...@googlegroups.com
For start you don't need to specify the primary key (orgid for example) [1]
In foreingkeys you need to use related names [2]

In views use select_related() [3]

--

Chris Strasser

unread,
Jul 6, 2015, 12:04:09 PM7/6/15
to django...@googlegroups.com
I tried and this happens:

from main.models import Contact

>>> contact = Contact.all()[0]

Traceback (most recent call last):

  File "<console>", line 1, in <module>

AttributeError: type object 'Contact' has no attribute 'all'

>>> 

?? 

any ideas ?


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/YYM-cTEZOgA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Vijay Khemlani

unread,
Jul 6, 2015, 12:07:54 PM7/6/15
to django...@googlegroups.com
Try with

Contact.objects.all()[0]

Florian Schweikert

unread,
Jul 6, 2015, 12:51:46 PM7/6/15
to django...@googlegroups.com
On 06/07/15 14:04, Chris Strasser wrote:
> my problem is with reverse lookups (i think it is called) the docs say that i can access entries through blog.entry,(by lowercaseing the class)
> it appears to me that i have the same setup with contact and location but i am getting errors ??

I think you forgot about "_set".
Try something like:

Location.contact_set.all()

And please don't use AutoField, db_column and primary_key until you have
a good reason. Also ForeignKey fetches an object of the connected type,
not an id, so "contactid", ... is confusing ;)

--
Florian

Rafael E. Ferrero

unread,
Jul 6, 2015, 2:25:27 PM7/6/15
to django...@googlegroups.com
contacts = Contacts.objects.select_related()

--
Rafael E. Ferrero

--
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 http://groups.google.com/group/django-users.

Chris Strasser

unread,
Jul 6, 2015, 7:16:20 PM7/6/15
to django...@googlegroups.com
WOO HOO !.. success ... thanks everyone...
 just needed a nudge in the right direction .... I will tackle the templates tomorrow and see where that takes me.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/YYM-cTEZOgA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Rafael E. Ferrero

unread,
Jul 7, 2015, 7:46:20 AM7/7/15
to django...@googlegroups.com
Nice!! Chris good work !!

--
Rafael E. Ferrero

Reply all
Reply to author
Forward
0 new messages