Saturday, April 23, 2016

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

On Saturday, April 23, 2016 at 10:00:45 AM UTC-4:30, 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 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 

Hi,
1.  Do django model fields default to required unless you use blank=True, null=True?
This may help you understand that: http://stackoverflow.com/questions/8609192/differentiate-null-true-blank-true-in-django

2. I think you need a foreign key on Contact to Organization:

class Contact(models.Model):
    name = ....
    ....
    organization = models.ForeignKey('Organization', null=True, blank=True)

That way you can have many contacts to a single organization. The contact_name in Organizations allows only 1 Contact, but you said you need many.

3. I cannot create connections without specifying a contact.
I don't see why not. Do you get any error?

4. If I was adding a contact using the admin interface, how do I allow no value for that foreign field
By setting null=True, blank=True for the ForeignKey field. Notice Contact do not have any ForeignKey in your example.

5. or allow for some kind of ajax type of text completion?
I have used this in the past: https://django-autocomplete-light.readthedocs.org/en/master/

6.  I would like to support multiple connection types - e.g. following, employer, etc.
You already have a 'type' field in Connections model. First I suggest to use a different name for the field: type is a reserved Python word.
Second, you can add choices to the field.

from django.utils.translation import ugettext as _
...
class Connection(models.Model):
    CHOICES = (('follower', _('Follower')),
               ('employer', _('Employer')),
               ('unspecified', _('Unspecified')),
              )
    connection_type = models.CharField(max_length=20, choices=CHOICES)
    contact_name = models.ManyToManyField(Contact)  



--
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/6918823e-82e5-4d7d-86c5-07b2dfb9db62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment