Monday, January 23, 2012

Re: retrieve model instance from database without model instance

Check out the content types framework[1].

You can do something like:

ct = ContentType.objects.get(app_label='products', model='product')
obj = ct.get_object_for_this_type(name='Venezuelan Beaver Cheese')

However, as you can see in the example, you'll need to know the name of
the app so that Django knows where to find your model.

1. https://docs.djangoproject.com/en/1.3/ref/contrib/contenttypes/

Brett


On 1/23/12 3:15 PM, "brian" <brian.downing@yakkadesign.com> wrote:

>How do I retrieve a model instance from the database without a model
>instance?
>
>For example from the doc[1] it has:
> product = Product.objects.get(name='Venezuelan Beaver Cheese')
>
>I want something like getInstance( 'Product', 'Venezuelan Beaver
>Cheese' )
>
>Here are the details of what I'm doing. I have multiple form models
>that are all handled by the same post processing class. I'm using
>django-celery to do the processing.
>
>I want to do something like this in my view:
> dbSave = form.save()
> processTask.delay('Product', dbSave.pk)
>
>Then in the task:
> class processTask(Task):
> def run(self, modelName, pk):
> instance = getInstance( modelName, pk )
> Š.
>
>From what I've read about django-celery, I shouldn't pass class
>instances to the task so I need a way to pass/retrieve this info via
>basic datatypes.
>
>Brian
>
>
>[1]
>https://docs.djangoproject.com/en/dev/ref/models/instances/#updating-attri
>butes-based-on-existing-fields
>
>--
>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.
>

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate