Changes between Initial Version and Version 1 of Ticket #2316, comment 1


Ignore:
Timestamp:
Feb 21, 2011, 11:12:51 AM (13 years ago)
Author:
Luke Plant

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #2316, comment 1

    initial v1  
    33
    44Ugly workaround (thats the reason I posted in the first place ;-):
     5{{{
     6#!python
    57from django.contrib.contenttypes.models import ContentType
    6 baz = Bar.objects.get_or_create(content_type=ContentType.objects.get_for_model(foo.__class__), object_id=getattr(foo, foo.__class__._meta.pk.column))
     8baz = Bar.objects.get_or_create(content_type=ContentType.objects.get_for_model(foo.__class__),
     9                                object_id=getattr(foo, foo.__class__._meta.pk.column))
     10}}}
    711
    812Somehow nicer (but unflexible in this form):
     13{{{
     14#!python
    915def do_generic_stuff(func, obj):
    10         from django.contrib.contenttypes.models import ContentType
    11         content_type = ContentType.objects.get_for_model(obj.__class__)
    12         object_id = getattr(obj, obj.__class__._meta.pk.column)
    13         return func(content_type=content_type, object_id=object_id)
     16    from django.contrib.contenttypes.models import ContentType
     17    content_type = ContentType.objects.get_for_model(obj.__class__)
     18    object_id = getattr(obj, obj.__class__._meta.pk.column)
     19    return func(content_type=content_type, object_id=object_id)
     20
    1421# call:
    1522baz = do_generic_stuff(Bar.objects.get_or_create, foo)
     23}}}
Back to Top