Reputation: 8164
Something is wrong with my models,or in the way I am trying to implement my algorithm. models.py
class Profile(models.Model):
username = models.OneToOneField(User, on_delete=models.CASCADE)
password = models.TextField(max_length=80,blank=True)
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)
Upload function(views.py)
@login_required
def upload(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile=request.FILES['docfile'])
profile = Profile.objects.get(user = request.user)
newdoc.uploaded_by = profile.request.user
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('upload'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render(request,'upload.html',{'documents': documents, 'form': form})
I got fielderror at upload
Cannot resolve keyword 'user' into field. Choices are: document, id, password, username, username_id
It points to line 57
profile = Profile.objects.get(user = request.user)
If I change this line
profile = Profile.objects.get(username = request.user)
I have this error
Exception Value:
Profile matching query does not exist.
Is it possible to get more information form shell? This works so far
In [3]: user = User.objects.get(username='drazen75')
In [6]: user.last_login
Out[6]: datetime.datetime(2018, 6, 24, 7, 1, 2, 196411, tzinfo=<UTC>)
My db schema
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);
EDIT Daniel suggested that I change profile field
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
password = models.TextField(max_length=80,blank=True)
Done,also migrations. Again same problem.
File "/home/milenko/nup/malex/views.py" in upload
57. profile = Profile.objects.get(user = request.user)
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 get
403. self.model._meta.object_name
Exception Type: DoesNotExist at /upload/
Exception Value: Profile matching query does not exist.
sqlite schema output .schema malex_profile
CREATE TABLE IF NOT EXISTS "malex_profile" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "password" text NOT NULL, "user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED);
How to fix this?
Upvotes: 0
Views: 139
Reputation: 599550
For some reason you've called the field that links Profile to User username
. You could change your query to use username
instead, but really you should fix the field; it refers to the User, so it should be called user
.
Upvotes: 1