Changes between Version 8 and Version 9 of CookBookNewformsAdminAndUser


Ignore:
Timestamp:
Aug 9, 2008, 2:21:23 PM (16 years ago)
Author:
oyvind
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CookBookNewformsAdminAndUser

    v8 v9  
    1717    content = models.TextField()
    1818
     19class Comment(models.Model):
     20    post = models.ForeignKey(Post)
     21    user = models.ForeignKey(User)
     22    content = models.TextField()
     23
    1924# app/admin.py
    2025
    21 from app.models import Post
     26from app.models import Post, Comment
    2227from django.contrib import admin
     28
     29class CommentInline(admin.TabularInline):
     30    model = Comment
     31    fields = ('content',)
    2332
    2433class PostModelAdmin(admin.ModelAdmin):
    2534
    2635    fields= ('content',)
     36    inlines = [CommentInline]
    2737
    2838    def save_model(self, request, form, change):
    2939
    30         new_object = form.save(commit=False)
     40        instance = form.save(commit=False)
    3141
    32         new_object.user = request.user
    33         new_object.save()
     42        instance.user = request.user
     43        instance.save()
    3444
    3545        form.save_m2m()
    3646       
    37         return new_object
     47        return instance
     48
     49    def save_formset(self, request, formset, change):
     50
     51        instances = formset.save(commit=False)
     52
     53        for instance in instances:
     54
     55            instance.user = request.user
     56            instance.save()
     57
     58        formset.save_m2m()
    3859
    3960admin.site.register(Post, PostModelAdmin)
Back to Top