WebDec 4, 2014 · from django.db.models import get_model user_model = get_model ('auth', 'user') To use your example exactly: ctype = ContentType.objects.get (model='user') related_to_user = Room.objects.filter (content_type=ctype) Share Improve this answer Follow edited Feb 25, 2011 at 17:13 answered Feb 25, 2011 at 16:57 Yuji 'Tomita' … WebSince ContentType objects are automatically created by Django during the database synchronization process, the primary key of a given content type isn’t easy to predict; it …
python - How do I get the object if it exists, or None if it does not ...
WebApr 18, 2024 · So if you look at the last point in your traceback, you'll see that your error is coming from this line main, sub = avatar.content_type.split ('/') , which appears to be from your clean_avatar method. Looks like you're trying to make sure it's an image... I have to imagine there's another way to do that. WebAug 3, 2009 · from django.contrib.contenttypes.models import ContentType ct = ContentType.objects.get_for_id (content_type) obj = ct.get_object_for_this_type (pk=object_id) Share Follow answered Aug 3, 2009 at 8:46 Wogan 68.9k 5 33 35 11 I'd like to point out that calling the argument content_type is somewhat misleading. get_for_id … ronald koeman olaf thon
How to get the app a Django model is from? - Stack Overflow
WebJul 21, 2014 · >>> from django.contrib.contenttypes.models import ContentType >>> ct = ContentType.objects.get (model='user') >>> ct.model_class () >>> ct_class = ct.model_class () >>> ct_class.username = 'hellow' >>> ct_class.save () TypeError: unbound method save () must be called with User instance as first argument (got nothing instead) … WebDec 1, 2024 · Ok well the direct answer to your question: ( from the django source code ) is: Media Types parsing according to RFC 2616, section 3.7. Which is the tears way of saying that it reads/allows-you-to-modify/passes along the 'Content-type' httpd header. However, you are asking for a more practice usage example. I have 2 suggestions for you: WebJun 22, 2010 · From django docs get () raises a DoesNotExist exception if an object is not found for the given parameters. This exception is also an attribute of the model class. The DoesNotExist exception inherits from django.core.exceptions.ObjectDoesNotExist You can catch the exception and assign None to go. ronald knox new testament