Monday, July 6, 2015

Re: Models and relationships

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]

[1] https://docs.djangoproject.com/en/1.8/ref/models/fields/#primary-key
[2] https://docs.djangoproject.com/en/1.8/ref/models/fields/#foreignkey
[3] https://docs.djangoproject.com/en/1.8/ref/models/querysets/#select-related
--
Rafael E. Ferrero

2015-07-06 9:04 GMT-03: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.

--
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/CAJJc_8XiTUaeXBwCwKuqO8L7gnX8muqWd7pjGchT0C_FnDVCQA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment