Wednesday, June 27, 2012

Re: ManyToManyField is this right?

Thanks Tom. Is this the example your talking about, https://docs.djangoproject.com/en/1.4/topics/db/models/#intermediary-manytomany ? If so, I think this should work...

# Different groups and states that issue teaching certificates/licenses
class IssuingAuthority(models.Model):
    name = models.CharField(max_length=254)
    state = USStateField()
    date_added = models.DateTimeField(auto_now_add=True)

# Certifications issued related to IssuingAuthority
class Certifications(models.Model):
    certs = models.ManyToManyField(License_Types, through='UserProfile')
    name = models.CharField(max_length=254)
    date_added = models.DateTimeField(auto_now_add=True)

# User Profile Model
class UserProfile(models.Model):
    user = models.OneToOneField(User)
    address_1 = models.CharField(max_length=254)
    address_2 = models.CharField(max_length=254)
    state = USStateField()
    zip = models.IntegerField(max_length=7)
    phone = PhoneNumberField()
    issuing_authority = models.ForeignKey(IssuingAuthority)
    certification = models.ForeignKey(Certifications)

I'll try to load that up later (most likely tomorrow) and see if I can get it to work.

Thanks much.

On Wed, Jun 27, 2012 at 5:27 PM, Thomas Lockhart <tlockhart1976@gmail.com> wrote:
On 6/27/12 11:48 AM, David Wagner wrote:
Looking at that I think I may need to add a foreignkey to cert_types relating to person since a person can have multiple certification types (NRA Instructor, CCL Instructor, etc).
No. See below.


On Wed, Jun 27, 2012 at 11:40 AM, David Wagner <cptnwinky@gmail.com> wrote:
i think I may just be over thinking this. The last time I did any significant coding for the web as pre-php5 and so this whole MVC thing is something to adapt too for sure.
Yes you are :)


I think I need to start thinking of the Model in the same way I would design a database back in the day with phpMyAdmin. I think I'm getting bogged down in trying to understand how it will relate to the View. Perhaps I need to just put the View out of my mind for the time being.
Yes. If you've done databases (that is some of my background too) then the models (always the first step here) should become comfortable fairly quickly, even if you are rusty. See below...


So thinking of this as just a database schema it would be something like (in psuedo-code)....

cert_types
  • type
  • date_created

certs

  • type = foreignkey(cert_types)
  • name
  • state (optional)
  • date_created

person

  • name
  • etc...
  • certificates = foreignkey(certs)
OK, "cert_types" is good (or at least a good start). "person" needs to have a manytomany on cert_types rather than a foreign key on certs. And "certs" can be the explicit intermediate table used for the manytomany relationship and can hold things like dates. Look at the django docs on how to explicitly define the intermediate table, but it is in the musician and musical group example; google for "paul ringo django extra fields" :)

hth

                             - Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment