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=9ef806b4972c4263ff5671ce; HttpOnly; Path=/; Secure
set-cookie: trac_session=59f02561c3d22b928c68f516; expires=Tue, 21 Oct 2025 05:44:52 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: Wed, 23 Jul 2025 05:44:52 GMT
x-served-by: cache-fra-eddf8230108-FRA, cache-bom-vanm7210035-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753249492.175684,VS0,VE261
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.