CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/html;charset=utf-8
cache-control: must-revalidate
expires: Fri, 01 Jan 1999 00:00:00 GMT
set-cookie: trac_form_token=c4c851064bd0dece11de6a8a; HttpOnly; Path=/; Secure
set-cookie: trac_session=518000e217d42bdfd8b2888c; expires=Wed, 22 Oct 2025 19:01:25 GMT; HttpOnly; Path=/; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
permissions-policy: interest-cohort=()
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
accept-ranges: bytes
via: 1.1 varnish, 1.1 varnish
date: Thu, 24 Jul 2025 19:01:25 GMT
x-served-by: cache-fra-eddf8230108-FRA, cache-bom-vanm7210082-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753383686.818681,VS0,VE171
vary: Accept-Encoding
CookBookNewformsAdminAndUser – Django
Back to Top
Django
The web framework for perfectionists with deadlines.
Issues
How to set the current user on the model instance in the admin:
# app/models.py from django.db import models from django.contrib.auth.models import User class Post(models.Model): user = models.ForeignKey(User) content = models.TextField() class Comment(models.Model): post = models.ForeignKey(Post) user = models.ForeignKey(User) content = models.TextField() # app/admin.py from app.models import Post, Comment from django.contrib import admin class CommentInline(admin.TabularInline): model = Comment fields = ('content',) class PostAdmin(admin.ModelAdmin): fields= ('content',) inlines = [CommentInline] def save_model(self, request, obj, form, change): obj.user = request.user obj.save() def save_formset(self, request, form, formset, change): if formset.model == Comment: instances = formset.save(commit=False) for instance in instances: instance.user = request.user instance.save() else: formset.save() admin.site.register(Post, PostAdmin)
Last modified
14 years ago
Last modified on Oct 12, 2011, 9:57:18 AM
Note:
See TracWiki
for help on using the wiki.
Download in other formats:
Django Links
Learn More
Get Involved
Follow Us
- Hosting by In-kind donors
- Design by Threespot &
© 2005-2025 Django SoftwareFoundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.