You can use Q objects for this. They can be negated with the ~
operator and combined much like normal Python expressions:
from myapp.models import Entry
from django.db.models import Q
Entry.objects.filter(~Q(id=3))
will return all entries except the one(s) with 3
as their ID:
[<Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, ...]
https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render
render(request, template[, dictionary][, context_instance][, content_type][, status][, current_app])
render()
is a brand spanking new shortcut for render_to_response
in 1.3 that will automatically use RequestContext
that I will most definitely be using from now on.
2020 EDIT: It should be noted that render_to_response()
was removed in Django 3.0
https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render-to-response
render_to_response(template[, dictionary][, context_instance][, mimetype])ΒΆ
render_to_response
is your standard render function used in the tutorials and such. To use RequestContext
you'd have to specify context_instance=RequestContext(request)
https://docs.djangoproject.com/en/1.8/ref/generic-views/#django-views-generic-simple-direct-to-template
direct_to_template
is a generic view that I use in my views (as opposed to in my urls) because like the new render()
function, it automatically uses RequestContext
and all its context_processor
s.
But direct_to_template
should be avoided as function based generic views are deprecated. Either use render
or an actual class, see https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/
I'm happy I haven't typed RequestContext
in a long, long time.
Best Answer
Differences between
OneToOneField(SomeModel)
andForeignKey(SomeModel, unique=True)
as stated in The Definitive Guide to Django:In contrast to the
OneToOneField
"reverse" relation, aForeignKey
"reverse" relation returns aQuerySet
.Example
For example, if we have the following two models (full model code below):
Car
model usesOneToOneField(Engine)
Car2
model usesForeignKey(Engine2, unique=True)
From within
python manage.py shell
execute the following:OneToOneField
ExampleForeignKey
withunique=True
ExampleModel Code