我正在构建一个用于上传文件的Django应用程序。我希望能够计算用户拥有的文件数量以及批准的文件数量。我有一个布尔'批准的模型'领域。此模型位于admin.py中。
在admin.py
中from django.contrib import admin
from .models import FileModel
from .forms import FileForm
class FileModelAdmin(admin.ModelAdmin):
form = FileForm
fields = ('title', 'description', 'categories', 'pub_date', 'submitted_date', 'author', 'user', 'approved', 'upload', 'vote')
# pass
list_display = ['title', 'approved', 'author', 'user', 'categories', 'description', 'pub_date', 'submitted_date', 'upload', 'vote']
admin.site.register(FileModel, FileModelAdmin)
我想要另一个模型中的整数' new_user_model',这是在另一个应用中,以增加整数字段'文件'在管理站点中批准文件时使用一个。通过这种方式,我可以跟踪每个用户已批准的文件数量。
class NewUserModel(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
# user = models.ForeignKey(User)
files = models.IntegerField(default=0)
文件模型
class FileModel(models.Model):
title = models.CharField(max_length=100)
description = models.CharField(max_length=255)
pub_date = models.DateTimeField('date published')
submitted_date = models.DateTimeField('date submitted')
author = models.CharField(max_length=255)
user = models.ForeignKey(User, default=6)
approved = models.BooleanField(default=False)
upload = models.FileField().....................
...................................................
因此,当文件被批准时,我希望new_user_model中的files字段增加1。并在管理站点中点击保存按钮。此外,如果批准'如果没有选中,我需要'文件'字段减少一个。
怎么可以这样做?
更新:
最终,我希望能够显示用户在网站上拥有的文件数量。
谢谢,
答案 0 :(得分:1)
您可以添加custom admin action,将文件标记为已批准。这些传递了一个请求对象,您可以使用它来抓取if(returnValue != "error") {
self.delegate?.userLoggedIn(data: userEmail!)
UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
self.dismiss(animated: true, completion: nil)
}
。 (您可能想要从您的模型名称中删除“模型”,似乎有点多余,但我想个人品味:)
NewUserModel
话虽如此,如果你确实想跟踪谁批准了什么:
class FileModelAdmin(admin.ModelAdmin):
form = FileForm
fields = ('title', 'description', 'categories', 'pub_date', 'submitted_date', 'author', 'user', 'approved', 'upload', 'vote')
# ....
actions = ['approve_files']
def approve_files(self, request, queryset):
# Queryset can be greater than 1
queryset.update(approved=True)
user = request.user
# I keep wanting to put underscores in...
user_files_count = user.newusermodel.files
# Get the amount of Files in the query, may be more than one
user.newusermodel.files = user_files_count + queryset.count()
user.newusermodel.save()
然后上面的管理操作就是:
class FileModel(models.Model):
title = models.CharField(max_length=100)
# ...
approved_by = models.ForeignKey('auth.User')
您可以通过执行以下操作来获取用户的文件数:
def approve_files(self, request, queryset):
# Queryset can be greater than 1
queryset.update(approved_by=request.user)
答案 1 :(得分:0)
我终于有机会完成这个?
from django.contrib import admin
from .models import FileModel
from django.contrib.auth.models import User
from .forms import FileForm
from crudapp.models import NewUserModel
#from django.db import models
from django.core.exceptions import PermissionDenied
from django.contrib import admin
from django.contrib.admin.actions import delete_selected as delete_selected_
# see https://gist.github.com/rudyryk/4190318
# overrides and recreates delete_selected
def delete_selected(modeladmin, request, queryset):
if not modeladmin.has_delete_permission(request):
raise PermissionDenied
if request.POST.get('post'):
for obj in queryset:
obj.delete()
else:
return delete_selected_(modeladmin, request, queryset)
delete_selected.short_description = "Delete selected objects"
class FileModelAdmin(admin.ModelAdmin):
form = FileForm
fields = ('title', 'description', 'categories', 'pub_date', 'submitted_date', 'author', 'user', 'approved', 'upload', 'vote')
# pass
list_display = ['title', 'approved', 'author', 'user', 'categories', 'description', 'pub_date', 'submitted_date', 'upload', 'vote']
actions = ['approve_files', 'delete_selected']
def approve_files(self, request, obj):
for qs in obj:
print "run number..."
author = qs.author
adminApproved = qs.approved # approved status on admin site
userInstance = User.objects.get(username=author)
id = qs.id
item = FileModel.objects.get(pk=id)
approved = item.approved # approved status in FileModel
id = userInstance.id
item = NewUserModel.objects.get(user_id=id)
print "existentialism"
print "approved: %s" %(approved)
print "adminApproved: %s" %(adminApproved)
if(approved == False):
item.files += 1
item.save()
obj.update(approved = True)
#NewUserModel.save()
#obj.save()
def save_model(self, request, obj, form, change):
id = obj.id
item = FileModel.objects.get(pk=id)
# Test if file 'approved' has been changed on admin site
if(item.approved != obj.approved):
author = obj.author
userInstance = User.objects.get(username=author)
id = userInstance.id
item = NewUserModel.objects.get(user_id=id)
if(obj.approved == True):
item.files += 1
item.save()
else:
item.files -= 1
item.save()
obj.save()
def delete_model(self, request, queryset):
print "we are inside delete_model()"
author = queryset.author
userInstance = User.objects.get(username=author)
id = queryset.id
item = FileModel.objects.get(pk=id)
approved = item.approved # approved status in FileModel
#filename=obj.profile_name+".xml"
#os.remove(os.path.join(obj.type,filename))
id = userInstance.id
item = NewUserModel.objects.get(user_id=id)
# -1 for file in filemodel only if approved
if approved == True:
item.files -= 1
item.save()
print "inner part of delete_model()"
queryset.delete()
# The 'Delete selected' action
def delete_selected(self, request, queryset):
print "we are inside delete_selected()"
for obj in queryset:
author = obj.author
adminApproved = obj.approved # approved status on admin site
userInstance = User.objects.get(username=author)
id = obj.id
item = FileModel.objects.get(pk=id)
approved = item.approved # approved status in FileModel
id = userInstance.id
item = NewUserModel.objects.get(user_id=id)
# For the case where a file is deleted but was approved yet
if approved == True:
item.files -= 1
print "inner part of delete_selected()"
item.save()
print "running for each iteration of obj"
queryset.delete()
admin.site.register(FileModel, FileModelAdmin)