我一直在努力统计用户上传的文件 models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
class Document(models.Model):
docfile = models.FileField(upload_to='documents/%Y/%m/%d')
uploaded_by = models.ForeignKey(Profile,on_delete=models.CASCADE)
date_uploaded = models.DateTimeField(auto_now_add=True)
class UploadDetail(models.Model):
profile = models.ForeignKey(Profile,on_delete=models.CASCADE)
document = models.ForeignKey(Document,on_delete=models.CASCADE)
amount = models.IntegerField(default=0)
视图上传功能
@login_required
def upload(request):
# Handle file upload
user = request.user
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile=request.FILES['docfile'])
newdoc.uploaded_by = request.user.profile
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('upload'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the upload page
documents = Document.objects.all()
current_up = Document.objects.filter(uploaddetail=id).count()
追踪
Traceback:
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
21. return view_func(request, *args, **kwargs)
File "/home/milenko/mk/malex/views.py" in upload
47. current_up = Document.objects.filter(uploaddetail=id).count()
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/query.py" in filter
836. return self._filter_or_exclude(False, *args, **kwargs)
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/query.py" in _filter_or_exclude
854. clone.query.add_q(Q(*args, **kwargs))
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/sql/query.py" in add_q
1253. clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/sql/query.py" in _add_q
1277. split_subq=split_subq,
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/sql/query.py" in build_filter
1215. condition = self.build_lookup(lookups, col, value)
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/sql/query.py" in build_lookup
1085. lookup = lookup_class(lhs, rhs)
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/lookups.py" in __init__
18. self.rhs = self.get_prep_lookup()
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/fields/related_lookups.py" in get_prep_lookup
115. self.rhs = target_field.get_prep_value(self.rhs)
File "/home/milenko/miniconda3/lib/python3.6/site-packages/django/db/models/fields/__init__.py" in get_prep_value
947. return int(value)
Exception Type: TypeError at /upload/
Exception Value: int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method'
这是迁移文件uploaddetaill的样子
class Migration(migrations.Migration):
dependencies = [
('malex', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='UploadDetail',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('amount', models.IntegerField(default=0)),
('document', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='malex.Document')),
('profile', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='malex.Profile')),
],
),
]
这是我的sqlite转储
sqlite> .dump malex_document
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "malex_document" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "docfile" varchar(100) NOT NULL, "date_uploaded" datetime NOT NULL, "uploaded_by_id" integer NOT NULL REFERENCES "malex_profile" ("id") DEFERRABLE INITIALLY DEFERRED);
INSERT INTO malex_document VALUES(1,'documents/2018/06/25/262_2016-06-16_17-12-37_2016-06-16_17-14-07_R000_131072H.xml','2018-06-25 16:23:25.991626',1);
INSERT INTO malex_document VALUES(2,'documents/2018/06/25/262_V01_C05_R000_TEx_BH_131072H.ats','2018-06-25 16:24:50.293830',1);
INSERT INTO malex_document VALUES(3,'documents/2018/06/25/262_V01_C06_R000_TEy_BH_131072H.ats','2018-06-25 16:26:03.728805',1);
INSERT INTO malex_document VALUES(4,'documents/2018/06/26/262_V01_C08_R000_THy_BH_131072H.ats','2018-06-26 10:43:23.603460',1);
INSERT INTO malex_document VALUES(5,'documents/2018/06/26/262_V01_C07_R000_THx_BH_131072H.ats','2018-06-26 18:55:31.047031',1);
CREATE INDEX "malex_document_uploaded_by_id_456f13bd" ON "malex_document" ("uploaded_by_id");
COMMIT;
Django指的是哪个内置函数? 如何解决这个问题? 我的模型怎么了?
答案 0 :(得分:1)
问题是id是<built-in function id>
@login_required
def upload(request):
...
# here is the wrong line, you pass `id` but you don't have this variable
# and django take his `id` function
current_up = Document.objects.filter(uploaddetail=id).count()
# maybe you need something like this
current_up = Document.objects.filter(uploaded_by=request.user.profile.id).count()