Thursday 15 September 2011

django - Displaying all fields of Foreign Key Model -



django - Displaying all fields of Foreign Key Model -

what want retrieve fields belonging model of foreign key.

my models example:

class baseproduct(models.model): name = models.charfield(max_length=256) variant = models.charfield(max_length=256, default='n/a') type = models.foreignkey(producttype) class producttype(models.model): name = models.charfield(max_length=256,blank=false,null=false) sofa = models.foreignkey(sofaproduct, blank=true, null=true) toaster = models.foreignkey(toasterproduct, blank=true, null=true)

these examples, there can number of producttype models each number of fields.

in template can display fields of baseproduct using baseproduct id. want display fields of fk.

for illustration if type = sofa in baseproduct, need retrieve , display sofa fields baseproduct fields.

(disclaimer: have tendency give long answers. you'll have forgive me that)

first rule of schema design - should reflect real world business logic (not actual business action mind you, implications of relationships). example, if have class person can create class pet foreginkey person translates - every person can have multiple pets.

if apply logic schema see producttype class has foreignkey both sofas , toasters, means each toaster can have multiple sofas , vice versa. lastly time checked, never heard of sofa had toaster.

in other words - need think you're trying accomplish here. i'm guessing baseproduct basic class has mutual fields, , sofa , toaster different types of products. since different, have own special fields, , shouldn't related, makes sense have them separate models. why need producttype? define name toaster? you're defining entire model! why need maintain name on different table (and not, say, custom method returns "i toaster, hear me roar")?

my best guess want able define new types of products on go. however, if intend maintain them separated on model level, you'll have create model each new product. , if want able simple define new model producttype, either need have 1 product class manage them all, or want complicated dynamic scheme can create new models on fly.

let's break options down:

create generic product , type class, did there:

class producttype(models.model): name = models.charfield(max_length=256,blank=false,null=false) class product(models.model): name = models.charfield(max_length=256) variant = models.charfield(max_length=256, default='n/a') type = models.foreignkey(producttype)

now each product can of 1 type, , can create new types on go. of course of study means product objects share same fields, , limiting. won't have same flexibility each type before (no sofa-only fields), on other hand easier create dynamic types of objects - define new producttype , bam have whole new grouping of products.

create basic abstract product model, , define new sub-model each new type of product. you'll have lot more flexibility each one, defining new types require defining new model , setting table it. scheme don't need producttype object @ because different models define different types (there's no need duplicity).

you can create kind of admin page process, it's not going easy setup, , might find many tables (which can problematic if need query on products - you'll have bring together lot of different tables, not efficient).

use non-relational database dynamic-models know how , disco*

*ok, it's more complicated that, explanation on how combine them way long, answer. if seems on head, forget it. if have thought how non-relation databases work, can figure out yourself

django database django-models

No comments:

Post a Comment