User:Andre Castro/2/django/manytomany: Difference between revisions

From XPUB & Lens-Based wiki
Line 33: Line 33:
'''ManyToManyField.through'''
'''ManyToManyField.through'''


The most common use for this option is <ul>when you want to associate extra data with a many-to-many relationship</ul>.
The most common use for this option is <u>when you want to associate extra data with a many-to-many relationship</u>.


Django will automatically generate a table to manage many-to-many relationships. However, if you want to manually specify the intermediary table, you can use the through option to specify the Django model that represents the intermediate table that you want to use.
Django will automatically generate a table to manage many-to-many relationships. However, if you want to manually specify the intermediary table, you can use the through option to specify the Django model that represents the intermediate table that you want to use.

Revision as of 13:59, 17 January 2013

I want to introduce a character model to the database of spam email messages I have been developing.

This will be a way to account for the characters in the spam narratives, either the ones described in them or narrating them.

The character Model

I must contain the following fields:

  • Name - CharField(max_length=200, unique=True)
  • Spam Messages - models.ManyToManyField(spam_msg)
  • Date of Birth - DateField(blank=True)
  • Date of Death - DateField(blank=True)
  • Nationality - CharField(blank=True, max_length=200)
  • Occupation - CharField(blank=True, max_length=200)


  • Biography - TextField(blank=True)
  • Appearances in media - TextField(blank=True)
  • Other - TextField(blank=True)


ManyToMany Relationship

ManyToManyField class ManyToManyField(othermodel[, **options])¶

A many-to-many relationship, requires a positional argument: the class to which the model is related. eg: Mr.Castro is relatated to spam_msg id=40, id=44

Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship. By default, this table name is generated using the name of the many-to-many field and the name of the table for the model that contains it.

many_to_many.gif


ManyToManyField.through

The most common use for this option is when you want to associate extra data with a many-to-many relationship.

Django will automatically generate a table to manage many-to-many relationships. However, if you want to manually specify the intermediary table, you can use the through option to specify the Django model that represents the intermediate table that you want to use.


ManyToManyField.db_table

The name of the table to create for storing the many-to-many data. If this is not provided, Django will assume a default name based upon the names of the two tables being joined.


ManyToMany(self): Relationships between characters

Eg:

Character A -- client-manager -- Character B
Character A -- business partner -- Character X
Character C -- lovers -- Character D

This is a reference to objects withing the same table/model. The relationship table must specified what is the affiliation between the 2 characters

http://stackoverflow.com/questions/11721157/django-many-to-many-m2m-relation-to-same-model


In the relationship table it has to indicated what is the 2 characters' afilitation

  • be ManyToMany: a character can be present in more that one email; one email can have more than one character.
  • the LINK TABLE must allow for the misspellings of the character's name (eg: Mr. Castro was spelled Mr. Costro in email#40)