Changes between Initial Version and Version 1 of Ticket #2316, comment 1
- Timestamp:
- Feb 21, 2011, 11:12:51 AM (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #2316, comment 1
initial v1 3 3 4 4 Ugly workaround (thats the reason I posted in the first place ;-): 5 {{{ 6 #!python 5 7 from 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)) 8 baz = 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 }}} 7 11 8 12 Somehow nicer (but unflexible in this form): 13 {{{ 14 #!python 9 15 def 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 14 21 # call: 15 22 baz = do_generic_stuff(Bar.objects.get_or_create, foo) 23 }}}