Monday, July 6, 2015

Re: Models and relationships

Try with

Contact.objects.all()[0]

On Mon, Jul 6, 2015 at 1:03 PM, Chris Strasser <cstrasser11@gmail.com> wrote:
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 ?


On Mon, Jul 6, 2015 at 8:43 AM, Sadaf Noor <email@sadafnoor.com> wrote:
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

2015-07-06 18:04 GMT+06:00 Chris Strasser <cstrasser11@gmail.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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAAJ2eVrtQ%2BpUkdW-OsAk0vQEHrWynwAAfOxQHtn%3D7xJ0AsEdHA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAH3cd3oHiNtBXN%2BR8UwWvNR6GThV8WPbCt4myEQs1cupdXp-SQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CALn3ei22_nRxjfx_E2be3YEeW1kyo6jgsogT7dgTX5XDbeBEMQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment