I'm doing a Django project (kind of social network) and want to have a page where I can see all my posts, which I did.
I allways get the error: no such column: uploaded_by
in models.py
from django.db import models
from django.contrib.auth.models import User
class ContentItem(models.Model):
upload_date = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=100, default='no title')
description = models.CharField(max_length=400, default='no description')
image = models.ImageField(upload_to='image_board/posts/', default='null')
uploaded_by = models.ForeignKey(User, default='0')
def __str__(self):
return self.title
in views.py
def view_my_favorites(request):
all_posts = ContentItem.objects.raw('SELECT * FROM image_board_ContentItem WHERE uploaded_by = request.user.username')
template = loader.get_template('favorites.html')
context = {
'all_posts': all_posts,
}
return HttpResponse(template.render(context, request))
I want to get the user name of the user who is loged in, how can i whrite this in the sql query?
Thaks guys :)
Your actual issue is probably caused by neglecting to make and run migrations after adding the uploaded_by field.
But there are a huge number of other things wrong here.
Firstly, you are comparing the uploaded_by column with a non-existent column, request.user.username. You need to use the actual value of that variable.
Secondly, you are comparing a foreign key - uploaded_by - with a string, username. These will never match.
Thirdly, you are using a raw query. There is absolutely no need to do that here.
Your query is trivial to express in the Django query syntax. You should do:
all_posts = ContentItem.filter(uploaded_by=request.user)
or even simpler:
all_posts = request.user.contentitem_set.all()
Related
I understand that you can't directly use icontains on a foreign key when searching but I haven't found a solution yet.
Here is my search view in views.py (I have imported every model needed):
def search(request):
# if the user actually fills up the form
if request.method == "POST":
searched = request.POST['searched']
# author__icontains part is not working
posts = Post.objects.filter(Q(title__icontains=searched) | Q(author__author__icontains=searched))
return render(request, 'blog/search.html', {'searched': searched, 'posts': posts})
else:
return render(request, 'blog/search.html', {})
Here is my model in model.py:
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
Mainly, this is not working:
posts = Post.objects.filter(Q(title__icontains=searched) | Q(author__author__icontains=searched))
The error is Related Field got invalid lookup: icontains
author is a User object. Therefore you should work with username, or first_name, or some other field. Likely author is also the value of a related_name=… [Django-doc] that thus makes a LEFT OUTER JOIN on another table, and thus would work on the primary key(s) of that table.
You thus filter with:
def search(request):
# if the user actually fills up the form
if request.method == 'POST':
searched = request.POST['searched']
# author__icontains part is not working
posts = Post.objects.filter(
Q(title__icontains=searched) |
Q(author__username__icontains=searched)
)
return render(request, 'blog/search.html', {'searched': searched, 'posts': posts})
return render(request, 'blog/search.html')
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
Note: Searching is usually done through a GET rquest, since that means the query is stored in the querystring and thus the URL. This makes it convenient to for example share the URL with the query to someone else, or bookmark the result. POST requests are usually used for state-changing actions, or for requests with sensitive data.
This problem is caused by Django's inability to handle foreign keys with multiple values for a single field. The reason for this limitation is that Django doesn't know how to resolve these conflicts, so it simply ignores them. In your case, since there are two fields in your model that match the search criteria, Django will ignore both of those results and display an empty list.
To fix this issue, we need to add a new attribute to our model called "icontains" which would contain the value of the other field. Then, we'll set this attribute as a default value for the "author" field when querying from the database. Here is what your model should look like now:
class Post(models.Model): title = models.CharField(max_length=100) content = models.TextField() date_posted = models.DateTimeField(default=timezone.now) author = models.ForeignKey(User, on_delete=models.CASCADE) icontains = models.CharField(max_length=100, null=True, blank=True) def __str__(self): return self.title def get_absolute_url(self): return reverse('post-detail', kwargs=dict(pk=self.pk))
With this change, the code will work properly.
For more information about this limitation, see the Django documentation here: https://docs.djangoproject.com/en/1.9/topics/db/queries/#lookups-that-span-relationships
Relatively new to Django, I'm working on a Django project and attempting to retrieve particular foreign key object into variable when it's selected in Form.
model.py
class item_category(models.Model):
idItemCat = models.CharField(primary_key=True max_length=5)
nameCategory = models.CharField(max_length=150)
def __str__(self):
return self.nameCategory
class item_code(models.Model):
idItemCat = models.ForeignKey(item_category, on_delete=models.DO_NOTHING)
idItemCode = models.CharField(primary_key=True, editable=False, max_length=20)
def __str__(self):
return self.idItemCode
I know I could retrieve object with making QuerySet such as .objects.last() and .objects.filter() or else, but it's just retrieve objects from database or existing data. What I'm about to do is, when a user submit a new data it'll retrieve particular foreign key object based on what I'm selected in this Form, so I could put into variable.
Any idea how should do it? it would be so much appreciated.
You really should look at the Django Docs on forms. They are excellent, and will teach you the right way to handle forms, a complicated topic.
To answer your question directly, it looks like you already have the html part, and as long as it has the form tag, like this:
<form action='your_view' method='post'>
...
</form>
Then in your view you could do something like this:
def your_view(request):
if request.method == 'POST':
category = item_category.objects.get(pk=request.POST.get('name_attribute_of_your_input'))
I'd need to see more specifics to give you a better answer, but there are several issues you should fix first.
First, your class names should be capitalized, field names lower case, and second,are you sure you want to make a CharField the primary key? You can, but for most cases, the automatically generated integer pk that Django creates is best.
class ItemCategory(models.Model):
# Django will create an integer pk for you
# idItemCat = models.CharField(primary_key=True max_length=5)
nameCategory = models.CharField(max_length=150)
def __str__(self):
return self.nameCategory
class ItemCode(models.Model):
idItemCat = models.ForeignKey(ItemCategory, on_delete=models.DO_NOTHING)
# Again, just let Django generate the primary key
# idItemCode = models.CharField(primary_key=True, editable=False, max_length=20)
def __str__(self):
return self.idItemCode
I am trying to create a search bar that searches for users in my Journal app. I want to redirect the search to the appropriate page if the user exists. I have created the view function, template and URL for the feature.
As a test, I imported the User class from django.contrib.auth.models and I was able to display a personalised message if the user existed.
def search_user(request):
"""Search for users."""
if request.method == "POST":
searched = request.POST["searched"]
usernames = User.objects.filter(username__icontains=searched)
context = {"searched": searched, "usernames": usernames}
return render(request, "journals/search_user.html", context)
In the main model, the Journal class has a ForeignKey relation with User and it is saved to a field called "owner". I would like to somehow check if the searched username matches with this "owner" field. When I try to make a search this way, it cannot find any data.
The reason I want the search to refer to "owner" is so that I can access the other fields in the table.
Could someone point out the mistake I am doing here?
from django.db import models
from django.contrib.auth.models import User
class Journal(models.Model):
"""A particular subject the user may want to write about."""
name = models.CharField(max_length=100)
date_added = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
"""Return a string representation of the model."""
return self.name
class Entry(models.Model):
"""Something specific the user may want to add to the journal."""
journal = models.ForeignKey(Journal, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
date_edited = models.DateTimeField(auto_now=True)
**View Function:**
def search_user(request):
"""Search for users."""
if request.method == "POST":
searched = request.POST["searched"]
usernames = Journal.objects.filter(owner=searched)
context = {"searched": searched, "usernames": usernames}
return render(request, "journals/search_user.html", context)
searched is just a text string, and Journal.owner is actually a User, so nothing will match here.
Match the related username instead ...
usernames = Journal.objects.filter(owner__username=searched)
As title says I was trying to sort a list of posts using the django order_by method and since the field I used was later added to the list (The field was not created inside the model) it failed.
Is there anything I can do about it other than adding the field to the model which is something I really don't wanna do?
Here is the model code
class post(models.Model):
title = models.CharField(max_length=236)
content = models.TextField()
post_board = models.ForeignKey(board, on_delete=models.CASCADE)
author = models.ForeignKey(User, on_delete=models.CASCADE)
release_date = models.DateField(auto_now_add=True)
views = models.IntegerField(default=0)
def __str__(self):
return self.title
The function that adds the extra field:
def forumdisplay(request, boardslug=None):
context = { 'board': None }
if boardslug:
context['board'] = board.objects.all().filter(slug=boardslug).first()
if context['board']:
context['posts'] = post.objects.all().filter(post_board=context['board'])
for eachpost in context['posts']:
eachpost.reply_count = len(reply.objects.all().filter(reply_to=eachpost))
eachpost.last_activity = eachpost.release_date
if eachpost.reply_count:
eachpost.last_activity = reply.objects.all().filter(reply_to=eachpost).order_by('release_date').first().release_date
context['posts'] = context['posts'].order_by('last_activity')
alter_posts(context['posts'])
else:
pass
return render(request, "board/forumdisplay.html", context)
The error I got:
Request Method: GET
Request URL: http://127.0.0.1:8000/forumdisplay/news/
Django Version: 3.0.4
Exception Type: FieldError
Exception Value:
Cannot resolve keyword 'last_activity' into field. Choices are: author, author_id, content, id, post_board, post_board_id, release_date, reply, title, views```
You can't.
order_by that you are trying to use is actually will be translated into SQL command to be executed on database, and while database has no column called last_activity so you can't apply this function to it.
what is the problem to add a new column to your DB and make it nullable?
I'm trying to do a search for a query in Django, the icontains lookup works for the title(CharField) but not for the content(TextField). I want to search if the query exists in the title or content of a post.
Model that I'm using to test queries: (models.py)
class BlogPost(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=120, blank=True)
content = models.TextField(blank=True)
timestamp = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '{} - {}'.format(self.title, self.user.username)
This is the code: (views.py)
from django.db.models import Q
...
class BlogPostAPIView(mixins.CreateModelMixin, generics.ListAPIView):
...
def get_queryset(self):
qs = BlogPost.objects.all()
query = self.request.GET.get('q')
if query is not None:
qs = qs.filter(Q(title__icontains=query) | Q(content__icontains=query)).distinct()
return qs
This is the error I get when making a query:
Exception Type: FieldError
Exception Value: Unsupported lookup 'icontains' for TextField or join on the field not permitted.
you can use
results = Cancer.objects.filter(abstract__contains='cancer').values_list('title', 'document_id')
str(results.query)
'SELECT "patents_cancer"."title", "patents_cancer"."document_id" FROM "patents_cancer" WHERE "patents_cancer"."abstract"::text LIKE %cancer%'
or
you can use Search for TextField
like Entry.objects.filter(body_text__search='Cheese')
https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/search/
Change your field to a CharField. As explained in a comment, you're using SQLite. SQLite doesn't pose any length restrictions on character fields.
You have two choices to solve this
1. Change TextField to CharField. This will compromise your text storage
2. Use DB such as MYSQL , PostgreSQL etc