计算每个用户已上传和批准的文件
Count files each user has uploaded and approved
我正在构建一个用于上传文件的 Django 应用程序。我希望能够计算用户拥有的文件数量和批准的文件数量。我有一个带有布尔 'approved' 字段的模型。此模型在 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' 中的整数,以便在管理站点批准文件时将整数字段 'files' 增加一个。这样我就可以跟踪每个用户批准了多少文件。
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 中的文件字段在文件为 'approved' 并且在管理站点中点击保存按钮时增加 1。此外,如果 'approved' 未选中,我需要 'files' 字段减少一个。
这是怎么做到的?
已更新:
最终我希望能够显示用户在站点上拥有的文件数。
谢谢,
您可以添加一个 custom admin action 来将文件标记为已批准。这些被传递给一个请求对象,您可以使用它来获取您的 NewUserModel
。 (您可能想从您的模型名称中删除 'Model',这似乎有点多余,但我想是个人品味 :)
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)
您可以通过以下方式获取用户的文件数:
user = Users.objects.get(id=some_id)
file_count = user.filemodel.all().count()
我终于有机会完成这个了?
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)
我正在构建一个用于上传文件的 Django 应用程序。我希望能够计算用户拥有的文件数量和批准的文件数量。我有一个带有布尔 'approved' 字段的模型。此模型在 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' 中的整数,以便在管理站点批准文件时将整数字段 'files' 增加一个。这样我就可以跟踪每个用户批准了多少文件。
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 中的文件字段在文件为 'approved' 并且在管理站点中点击保存按钮时增加 1。此外,如果 'approved' 未选中,我需要 'files' 字段减少一个。
这是怎么做到的?
已更新:
最终我希望能够显示用户在站点上拥有的文件数。
谢谢,
您可以添加一个 custom admin action 来将文件标记为已批准。这些被传递给一个请求对象,您可以使用它来获取您的 NewUserModel
。 (您可能想从您的模型名称中删除 'Model',这似乎有点多余,但我想是个人品味 :)
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)
您可以通过以下方式获取用户的文件数:
user = Users.objects.get(id=some_id)
file_count = user.filemodel.all().count()
我终于有机会完成这个了?
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)