Note that there are some explanatory texts on larger screens.

plurals
  1. POModel linking to other models based on field values
    primarykey
    data
    text
    <p>I'm working on simple ratings and comments apps to add to my project and am looking for advice on creating the models.</p> <p>Normally, I'd create these database schemas like this:</p> <pre><code>comment_ id - primary key type - varchar (buyer_item, buyer_vendor, vendor_buyer) source_id - int (primary key of the table based on the type) target_id - int (primary key of the table based on the type)) timestamp - timestamp subject - varchar comment - text rating_ id - primary key type - varchar (buyer_item, buyer_vendor, vendor_buyer) source_id - int (primary key of the table based on the type) target_id - int (primary key of the table based on the type) timestamp - timestamp rating - int (the score given, ie: 1-5 stars) </code></pre> <p>This would let me have simple methods that would allow me to apply comments or ratings to any type of thing by setting the proper type and setting the id's of who submitted it (source_id) what it applies to (target_id), like:</p> <pre><code>add_comment('user_product', user.pk, product.pk, now, subject, comment) add_comment('user_vendor', user.pk, vendor.pk, now, subject, comment) </code></pre> <p>I know in the models you define the relationships to other tables as part of the model. How would I define the relationship in these types of tables where the TYPE field determines what table the SOURCE_ID and TARGET_ID link to. </p> <p>Or should I omit the relationships from the model and set the joins up when I get the QuerySets?</p> <p>Or just trash the who common table idea and make a whole bunch of different tables to be used for each relationship (eg: user_ratings, product_ratings, transaction_ratings, etc)?</p> <p>What's the best practice here? My DBA senses say use common tables, but Django newbie me isn't sure what the natives do.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload