Tuesday, 15 March 2011

python - Using Generic Foreign Keys to retrieve data -



python - Using Generic Foreign Keys to retrieve data -

i have class streams in model.py supposed have generic foreign key class type (commentdata or responsedata)

when streams row want access info in class same id. instance, when create stream db entry create commentdata db entry same id , row have genericforeignkey commentdata. when want access comment info check related class stream , query content type rows id = stream.id

this in streams class:

limit = models.q(app_label='picture', model='commentdata') | models.q(app_label='picture', model='responsedata')` content_type = models.foreignkey(contenttype, verbose_name='data type', limit_choices_to=limit, null=true, blank=true) object_id = models.positiveintegerfield(verbose_name='related object', null=true, blank=true) content_object = genericforeignkey('content_type', 'object_id')

in django admin can savestreams content type either(commentdata or responsedata) fine.

if along lines of

x = streams.objects.all() y = x[0].content_type

i can output class streams related to. can't y.objects.all() related class error

manager isn't accessible via contenttype instances

is there anyway utilize contenttype manager find information? contenttype.objects.get_for_model(x[0]) returns class stream. contenttype.objects.get_for_model(y) returns content type.

thank you

ok. see you're doing. first, there no way trying using content_types api. can done.

as stated:

x = streams.objects.all() # stream objects y = x[0].content_type # commentdata contenttype object

so need utilize basic django orm this:

from django.db.models.loading import get_model x = streams.objects.all() # stream objects y = x[0].content_type # commentdata contenttype object model_class = get_model(app_label=y.app_label, model_name=y.model) model_class.objects.all() # objects of type

python django foreign-keys

No comments:

Post a Comment