Thursday, June 28, 2012

Re: ManyToManyField is this right?

On Wed, 27 Jun 2012 17:49:50 -0700, David Wagner <cptnwinky@gmail.com>
declaimed the following in gmane.comp.python.django.user:

Weird -- I spent nearly an hour last night responding to this, but
not only does it not show up in gmane, but I can't find it in either the
"drafts", "outbox", nor "sent" folders in Agent. So... this will be an
attempt to recreate my mind of three AM.

>
> # 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

Unless "License_Types" is a mistake for "IssuingAuthority" nothing
in Certifications is related to any 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)

Using a OneToOne here, if I read the Django pages properly
(confession, while I've had all the printed Django books, I've not
actually sat down to build anything), means that for each "User" entry,
there can be only ONE "UserProfile"! That means...

> address_1 = models.CharField(max_length=254)
> address_2 = models.CharField(max_length=254)
> state = USStateField()
> zip = models.IntegerField(max_length=7)

Where are you finding a 7 digit zip code. Mine is 49546-6356 --
that's 10 characters with the -, 9 if you format it at display time but
don't store the -. Also, since one does not do arithmetic with zip
codes, it should be a character field (zip 00102 as an integer will
display at 102, which is incorrect)

> phone = PhoneNumberField()
> issuing_authority = models.ForeignKey(IssuingAuthority)

... there will be only ONE issuing_authority and ...

> certification = models.ForeignKey(Certifications)

... only ONE certification per User...


Also, the way you have this defined, there is no constraint between
issuing_authority and certification. Nothing prevents you from having:

userProfile.state = Florida
userProfile.issuing_authority.name = "Department of Motor Vehicles"
userProfile.issuing_authority.state = "California"
userProfile.certification.name = "Michigan Commercial Drivers License"

See the problem?


Let's see if I can do it in Django syntax rather than just
relational database notation -- I'm not going to put in attributes like
field lengths:

#IssuingAuthority(_ID_, name, state)
class IssuingAuthority(models.Model):
name = models.CharField()
state = models.CharField()

#CertificateType(_ID_, name, /issuer/, dateAvailable, renewalPeriod)
class CertificateType(models.Model):
name = models.CharField()
issuer = models.ForeignKey(IssuingAuthority)
dateAvailable = models.DateField()
renewalPeriod = models.???() #days, months, years?

#User(_ID_, name, street, city, state, zip, phone)
class User(models.Model):
name = models.CharField()
street = models.CharField()
city = models.CharField()
state = models.CharField()
zip = models.CharField()
phone = models.CharField()
#stuff not in a regular relational schema description
certifications = models.ManyToMany(CertificateType,
through="Certification")

#Certification(_ID_, /user/, /type/, issueDate)
class Certification(models.Model):
user = models.ForeignKey(User)
type = models.ForeignKey(CertificateType)
issueDate = models.DateField()

--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/

--
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