Tuesday, April 26, 2016

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

OK, for starters, I understand the need for Null=True and blank=True.  So, jumping down...Not sure a better way to do this
but I will leave my comments for reference including my models and then respond to the questions....
So sorry for my delay, I wasn't well this past weekend.



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)   

>>>


Hi,
1.  Do django model fields default to required unless you use blank=True, null=True?

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.
>>> 
Ok that makes sense, I need a foreign key on the Contact table to link to the Organization.  I am not sure how easy it will be to show the results both ways 
that is ask for Contacts and see Orgs, or ask for Orgs and see all Contacts with the Org.  

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

This is something I thought people might say, "why do you want to do this?"  On Google Circles, you don't create circles typically and insert people into them.
Anyway, It seems to require that I pick a contact when I am creating a Connection.  Note, I used "connection" as a similar term to the way Google Plus uses "Circles" 
as that app seems to work well for me.  I will try again and see if it lets me save the connection without listing a contact. 
 
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.
>>>
As you stated, I do need to have a ForeignKey within Contacts probably for both Organizations and Connection tables. 

5. or allow for some kind of ajax type of text completion?

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)  

OK, that sounds great.  I debated over using choices but I can do a migrate at any time to get the Choices to allow 
more options... which isn't a difficult migration as nothing is changing about the schema per se, just the values allowed in
Choices.
Thanks,
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.
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/01c654a0-8d60-4d22-8282-c98dbb9ef1c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment