Saturday, April 23, 2016

Re: Help with defining Models for ManyToMany and OneToMany relationships...

On 23/04/2016 11:22 PM, Bruce Whealton wrote:
> Hello all,
> Â Â Â Â Â So, I setup django in a virtualenv on my Ubuntu
> environment. Â I was reading the docs and thought I had things rightÂ
> for creating the 3 models I wanted with this application. I am using
> Postgresql. Â I have the Postgresql driver for Python/Django installed
> in the virtualenv. Â It is a "Contacts" app. Â
> First question: Do django model fields default to required unless you
> use blank=True, null=True?
> Many of my fields, I want to have optional.
>
> I have a class called Contact, a class called Organization and a class
> called Connection. Â

I think you should rethink your Contact and Organization classes and see
if you can eliminate one or the other. A single table for both would
simplify the problem because the Connection class can implement as many
connections as you like.

For example ...

class ContactOrOrganization(etc):
various detail fields ...

class Connection(etc):
organization = ForeignKey("ContactOrOrganization",
related_name="organization")
contact = ForeignKey("ContactOrOrganization",
related_name="contact")

Just because I used related_name that way means nothing. You can connect
contacts together or organizations together. Also, you can add other
fields to Connection with which to describe the relationship.

Mike

> I wanted to use the Organization as a foreign key on the Contact model.
> Â I could have more than one contact from
> an Organization. Â The Connection model is inspired by the Google Plus
> idea of "Circles" - e.g. friends,
> family, following, etc. Â So, this would be a many-to-many relationship. Â
>
> My problems are (1) I cannot create connections without specifying a
> contact. Â
> (2) If I was adding a contact using the admin interface, how do I allow
> no value for that foreign field
> or allow for some kind of ajax type of text completion? Â If a person is
> family or friend, I may not need
> to list an Organization for them.
> (3) I would like to support multiple connection types - e.g. following,
> employer, etc.
>
> So, here is my apps models.py file:
> >>>>
>
> from django.db import models
>
>
> class Contact(models.Model):
> Â Â name = models.CharField(max_length=40)
> Â Â Organization = models.CharField(max_length=50)
> Â Â street_line1 = models.CharField("Street Line 1", max_length=50)
> Â Â street_line2 = models.CharField("Street Line 2", max_length=50)
> Â Â city = models.CharField(max_length=40)
> Â Â state = models.CharField(max_length=40)
> Â Â zipcode = models.CharField(max_length=20, blank=True, null=True)
> Â Â phone1 = models.CharField(max_length=20)
> Â Â phone2 = models.CharField(max_length=20)
> Â Â email = models.EmailField(max_length=60)
>
>
> class Organization(models.Model):
> Â Â name = models.CharField(max_length=60)
> Â Â street_line1 = models.CharField("Street Line 1", max_length=50)
> Â Â street_line2 = models.CharField("Street Line 2", max_length=50)
> Â Â city = models.CharField(max_length=40)
> Â Â state = models.CharField(max_length=40)
> Â Â zipcode = models.CharField(max_length=20, blank=True, null=True,)
> Â Â phone = models.CharField(max_length=20)
> Â Â email = models.EmailField(max_length=60)
> Â Â website = models.URLField(max_length=90)
> Â Â contact_name = models.ForeignKey(Contact, on_delete=models.CASCADE)
>
>
> class Connection(models.Model):
> Â Â type = models.CharField(max_length=60)
> Â Â contact_name = models.ManyToManyField(Contact) Â Â
>
> >>>
> Thanks in advance for any suggestions,
> BruceÂ
>
> --
> 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
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto: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/caede40b-640c-4e8d-997d-b76c62922c19%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/caede40b-640c-4e8d-997d-b76c62922c19%40googlegroups.com?utm_medium=email&utm_source=footer>.
> 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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/571C55CF.3070103%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment