Why don't I see my category ForeignKey field in related Model

39 views
Skip to first unread message

Bruce Whealton

unread,
May 11, 2016, 7:17:41 AM5/11/16
to Django users
Hello all,
           I think that my problem here is Django specific and not necessarily a reflection on
my understanding of relational databases (hopefully). 
I did post about this previously and thought I had figured out what to do. 
I have a Django app that stores information on Contacts. 
With that one table things seemed to work fine.  When I wanted to categorize
the type of relationship - is this a professional relationship, family, friends, etc.
That's when things didn't show up like I wanted.  I finally got the migration to work
with the new table. 
        I'm using python 3 with the latest version of django.  I have a mysql
database.  I want a one to many relationship, where one contact can
be characterized by many categories.  When I work with the django admin
and try to enter a contact, I'm not seeing a field for entering relationship categories.

So, here is my models.py for the contacts app.

from django.db import models


class Resource(models.Model):
    first_name = models.CharField(max_length=40)
    last_name = models.CharField(max_length=40)
    organization = models.CharField(max_length=60, null=True, blank=True)
    street_line1 = models.CharField("Street Line 1", max_length=50, null=True, blank=True)
    street_line2 = models.CharField("Street Line 2", max_length=50, null=True, blank=True)
    city = models.CharField(max_length=40, null=True, blank=True)
    state = models.CharField(max_length=40, null=True, blank=True)
    zipcode = models.CharField(max_length=20, blank=True, null=True)
    phone1 = models.CharField(max_length=20, null=True, blank=True)
    phone2 = models.CharField(max_length=20, null=True, blank=True)
    email = models.EmailField(max_length=60, null=True, blank=True)
    website = models.URLField(max_length=90, null=True, blank=True)

    def __str__(self):
        return "%s %s \t%s" % (self.first_name, self.last_name, self.organization)

    class Meta:
        ordering = ('last_name',)


class Relationship(models.Model):
    category = models.CharField(max_length=120)
    resource = models.ForeignKey(Resource, related_name='category')

    def __str__(self):
        return self.category

    class Meta:
        ordering = ('category',)

Thanks in advance for any help,
Bruce

Bill Freeman

unread,
May 11, 2016, 10:39:29 AM5/11/16
to django-users
This is a many to many relation.  One contact can have multiple relations, according to your text, but clearly, more than one contact can be family, etc.

And if, instead, each contact can have only one relation, then the ForeignKey goes in the Resource (contact) model, not the Relationship (category) model.

--
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...@googlegroups.com.
To post to this group, send email to django...@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/ab683e85-6945-4387-acd2-0ae3357db268%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bruce Whealton

unread,
May 13, 2016, 5:00:16 AM5/13/16
to Django users
You are absolutely right. So, I just need to figure out how to change things in the models.py. I'm not sure if django
needs a third model or not.
Thanks for helping where something should have been easier for me,
Bruce

Bill Freeman

unread,
May 13, 2016, 1:10:22 PM5/13/16
to django-users
If you use a ManyToManyField, Django creates the join table for you.  In the rare case that you need to store additional data on the join table, you can create your own and use ManyToManyField.through


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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages