Reference
Model field reference
Field types
If you want to be able to modify this field, set the following instead of auto_now_add=True: The default form widget for this field is a DateInput. The admin adds a JavaScript calendar, and a shortcut for “Today”.
Reference
Model field reference
Field types
See Managing files for details on how to provide this object. The default form widget for this field is a ClearableFileInput. Using a FileField or an ImageField (see below) in a model takes a few steps:
Reference
Model field reference
classField[source] : Field is an abstract class that represents a database table column. Django uses fields to create the database table (db_type()), to map Python types to database (get_prep_value()) and vice-versa (from_db_value()).
Reference
Model field reference
Fields often receive their values as a different type, either from serialization or from forms. `to_python(value)`[[source]](https://github.com/django/django/blob/main/django/db/models/fields/__init__.py#L799) : Converts the value into the correct Python object. It acts as the reverse of [`value_to_string()`](#django.db.models.Field.value_to_string), and is also called in [`clean()`](instances.md#django.db.models.Model.clean).
Reference
QuerySet API reference
QuerySet APIMethods that return new QuerySets
where / tables You can define explicit SQL WHERE clauses — perhaps to perform non-explicit joins — by using where. You can manually add tables to the SQL FROM clause by using tables.
Reference
Request and response objects
FileResponse objects
This is similar to the user attribute but it works in async contexts. HttpRequest.get_host()[source] : Returns the originating host of the request using information from the HTTP_X_FORWARDED_HOST (if USE_X_FORWARDED_HOST is enabled) and HTTP_HOST headers, in that order.
Reference
Request and response objects
FileResponse objects
HttpRequest.accepts(mime_type)[source] : Returns True if the request’s Accept header matches the mime_type argument: HttpRequest.read(size=None)[source] HttpRequest.readline()[source] HttpRequest.readlines()[source] HttpRequest.__iter__()[source] : Methods implementing a file-like interface for reading from an HttpRequest instance.
Topic guide
Using the Django authentication system
Authentication in web requestsRedirecting unauthorized requests in class-based views
If your AUTH_USER_MODEL inherits from AbstractBaseUser or implements its own get_session_auth_hash() method, authenticated sessions will include the hash returned by this function. In the AbstractBaseUser case, this is an HMAC of the password field.
Topic guide
Using the Django authentication system
Authentication in web requestsAuthentication Views
logout_then_login(request,login_url=None)[source] : Logs a user out on POST requests, then redirects to the login page. classPasswordChangeView[source] : URL name: password_change classPasswordChangeDoneView[source] : URL name: password_change_done classPasswordResetView[source] : URL name: password_reset
Topic guide
Using the Django authentication system
Authentication in web requestsAuthentication Views
classPasswordResetView[source] : URL name: password_reset
Topic guide
Introduction to class-based views
… Django uses to build class-based generic views are built for maximum flexibility, and as such have many hooks in the form of default method implementations and attributes that you are unlikely to be concerned with in the simplest use cases. …
Topic guide
Introduction to class-based views
… is worth noting that what your method returns is identical to what you return from a function-based view, namely some form of HttpResponse. This means that http shortcuts or TemplateResponse objects are valid to use inside a class-based view. While …
Topic guide
Using mixins with class-based views
Building up Django’s generic class-based views
The _detail part can be changed by setting template_name_suffix on a subclass to something else. (For instance, the generic edit views use _form for create and update views, and _confirm_delete for delete views.)
Topic guide
Using mixins with class-based views
First, let’s look at a naive attempt to combine DetailView with FormMixin to enable us to POST a Django Form to the same URL as we’re displaying an object using DetailView.
Topic guide
Models
FieldsRelationships
It doesn’t matter which model has the ManyToManyField, but you should only put it in one of the models – not both. Generally, ManyToManyField instances should go in the object that’s going to be edited on a form.
Topic guide
Creating forms from models
Model formsets
As with regular formsets, you can use the max_num and extra parameters to modelformset_factory() to limit the number of extra forms displayed.
Topic guide
Time zones
Concepts
Most websites that care about time zones ask users in which time zone they live and store this information in the user’s profile. For anonymous users, they use the time zone of their primary audience or UTC.
Topic guide
Translation
Internationalization: in template code
… within the translation block. Examples: You can use multiple expressions inside a single blocktranslate tag: NOTE: The previous more verbose format is still supported: > {% blocktranslate with book|title as book_t and author|title as author_t %} Other block tags (for …
Topic guide
Translation
Localization: how to create language files
NOTE: If you’re using Windows and need to install the GNU gettext utilities so > makemessages works, see gettext on Windows for more > information.
Topic guide
Testing tools
The test client
Use the django.test.Client class to make requests. classClient(enforce_csrf_checks=False,raise_request_exception=True,json_encoder=DjangoJSONEncoder,*,headers=None,query_params=None,**defaults)[source] : A testing HTTP client. Takes several arguments that can customize behavior.