How to make provide views and specify which users can GET it - django

I created the following model, with an author who is the user that created it and Title and description, with few other fields.
from django.db import models
from django.conf import settings
from django.utils import timezone
# Create your models here.
class Author(models.Model):
name = models.CharField(max_length=200)
added_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
created_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=200)
description = models.CharField(max_length=300)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
added_by = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
created_date = models.DateTimeField(default=timezone.now)
# 🔴 who_can_see
who_can_see = models.ManyToManyField(
settings.AUTH_USER_MODEL, related_name='tweet_user', blank=True)
def __str__(self):
return self.title
now I want to filter books based on who_can_see and auther
My question is: how to modify this books = Book.objects.filter(added_by=user) in order to get only the posts that have the authenticated username included in the who_can_see field.
in models.py i created this field to add the usernames that can see the view who_can_see = models.ManyToManyField( settings.AUTH_USER_MODEL, related_name='tweet_user', blank=True), but I don't know how to filter views based on them, also users can see all books even when they are not loggedin
#api_view(["POST"])
#csrf_exempt
#permission_classes([IsAuthenticated])
def add_book(request):
print({"request.data": request.data})
payload = request.data
user = request.user
try:
author = Author.objects.get(id=payload["author"])
book = Book.objects.create(
title=payload["title"],
description=payload["description"],
added_by=user,
author=author,
who_can_see=payload["who_can_see"] # 🔴
)
serializer = BookSerializer(book)
return JsonResponse({'books': serializer.data}, safe=False, status=status.HTTP_201_CREATED)
except ObjectDoesNotExist as e:
return JsonResponse({'error': str(e)}, safe=False, status=status.HTTP_404_NOT_FOUND)
except Exception:
return JsonResponse({'error': 'Something terrible went wrong'}, safe=False, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
#api_view(["GET"])
#csrf_exempt
#permission_classes([IsAuthenticated])
def get_books(request):
# here maybe I need other arguments than request to get the other filed, in order to create if statements.
user = request.user.id
# books = Book.objects.filter(added_by=user)
books = Book.objects.filter(who_can_see=user, added_by=user) # 🔴 after i created this user who are not logged in can see the all private books.
serializer = BookSerializer(books, many=True)
return JsonResponse({'books': serializer.data}, safe=False, status=status.HTTP_200_OK)

You have to write query in which you have to check for which book current user is in who_can_see field. You can write this like this.
from django.db.models import Q
current_user = request.user
books = Book.objects.filter(Q(Q(added_by=user) | Q(who_can_see=current_user)))
if you want to get books for which who_can_see is empty
books = Book.objects.filter(Q(Q(added_by=user) | Q(who_can_see=current_user) | Q(who_can_see=None)))

Related

How can I successful POST a Choice that is linked to a Question in a Poll's API in Django?

I am creating a survey / voting api, I can successfully POST question but I am getting the following error Choice() got an unexpected keyword argument 'question' when I try to create a choice that is linked to a question, I can't figure it out.
These are the models
from django.db import models
import datetime
from django.utils import timezone
# Модели для опоросов
class Question(models.Model):
poll_question = models.CharField(max_length=255, blank=False)
title = models.CharField(max_length=255, blank=True)
start_date = models.DateTimeField('date published', blank=False)
def __str__(self):
return self.poll_question
def published_not_longage(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.start_date <= now
#Модель для Выбора
class Choice(models.Model):
poll_question = models.ForeignKey(Question, on_delete=models.CASCADE)
poll_question_choice = models.CharField(max_length=255)
votes = models.IntegerField(default=0)
The choices serializer
class ChoiceSerializer(serializers.Serializer):
poll_question_choice = serializers.CharField(max_length=255)
def create(self, validated_data):
return Choice.objects.create(**validated_data)
#The choices view
- The error is arising when I pass in the question instance in the serializer.save() method.
#api_view(['POST'])
def choice_details(request, pk):
question = Question.objects.get(pk=pk)
# question = get_object_or_404(Question, pk=pk)
serializer = ChoiceSerializer(data=request.data)
if serializer.is_valid():
poll_question_choice = serializer.save(question=question)
return Response(ChoiceSerializer(poll_question_choice).data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
From the model Choice, the field is named poll_question so it needs to be used (as opposed to question) in save:
poll_question_choice = serializer.save(poll_question=question)

Autofill my author field with foreign key

I am trying to autofill my user foreign key in my note project with authentication in django. I tried, but it's not working and asking that owner is required field. Please, help! Thanks in an advance.
views.py
#login_required(login_url='login')
def index(request):
tasks = Task.objects.filter(owner=request.user)
form = TaskForm()
if request.method=='POST':
form = TaskForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.owner = request.user
instance.save()
context = {
'tasks':tasks,
'form':form,
}
return render(request, 'list.html',context)
models.py
class Task(models.Model):
title = models.CharField(max_length=200)
completed = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
def __str__(self):
return self.title
Since you fill in the owner yourself, it makes no sense to specify the owner as a form field. You thus should exclude it, and let this be handled by the view. The form thus looks like:
class TaskForm(forms.ModelForm):
class Meta:
model = Task
exclude = ['owner']
If no ModelForm will need to specify the owner, you can mark the field as non-editable:
class Task(models.Model):
# …
owner = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
editable=False
)
# …

My views.py is returning null from django admin object

I would really appreciate some help on this because I'm completely stuck. I've started up a simple django app (trying to make an instagram clone). However, when I try to display the post objects (which I created in the django admin page) nothing is displayed in index.html, so I tried printing out the objects in the views.py and it's returning to me an empty query set. I don't quite understand what I'm doing wrong and why I can't access the objects? When I print out the username I am able to get that, but then nothing for both post and stream objects. Please I'm so stuck any advice would be appreciated.
views.py
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.template import loader
from django.http import HttpResponse
# Create your views here.
from post.models import post, stream
#login_required
# we are getting all of the string objects that are created for the user
def index(request):
user = request.user
print(user)
posts = stream.objects.filter(user=user)
print(posts)
group_ids = []
#then looping through and getting post id to a list
for posted in posts:
group_ids.append(posted.post_id)
print(group_ids)
#then filtering them so that you can display it in the index
#selecting a specific post by id
post_items = post.objects.filter(id__in=group_ids).all().order_by('-date')
template = loader.get_template('index.html')
context = {'post_items' : post_items}
return(HttpResponse(template.render(context, request)))
models.py
from django.db import models
from django.contrib.auth.models import User
import uuid
# Create your models here.
from django.db.models.signals import post_save
from django.utils.text import slugify
from django.urls import reverse
def user_directory_path(instance,filename):
# this file is going to be uploaded to the MEDIA_ROOT /user(id)/filename
return('user_{0}/{1}'.format(instance.user.id,filename))
class tag(models.Model):
title = models.CharField(max_length = 80, verbose_name = 'tag')
slug = models.SlugField(null = False, unique = True)
class Meta:
verbose_name = 'tag'
verbose_name_plural = 'tags'
# for when people click on the tags we can give them a url for that
# def get_absolute_url(self):
# return(reverse('tags', args = [self,slug]))
def __str__(self):
return(self.title)
def save(self,*args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
return(super().save(*args, **kwargs))
class post(models.Model):
# will create a long id for each post
id = models.UUIDField(primary_key=True, default = uuid.uuid4, editable = False)
image = models.ImageField(upload_to = user_directory_path, verbose_name= 'image', null = True)
caption = models.TextField(max_length = 2000, verbose_name = 'caption')
date = models.DateTimeField(auto_now_add = True)
tags = models.ManyToManyField(tag, related_name='tags')
user = models.ForeignKey(User, on_delete=models.CASCADE)
likes = models.IntegerField()
def get_absolute_url(self):
return reverse('postdetails', args=[str(self.id)])
# def __str__(self):
# return(self.user.username)
class follow(models.Model):
follower = models.ForeignKey(User, on_delete=models.CASCADE, related_name='follower')
following = models.ForeignKey(User, on_delete=models.CASCADE, related_name='following')
class stream(models.Model):
following = models.ForeignKey(User, on_delete=models.CASCADE, related_name='stream_following')
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(post, on_delete=models.CASCADE)
date = models.DateTimeField()
def add_post(sender, instance,*args, **kwargs):
# here we are filtering all the users that are following you
post = instance
user = post.user
followers = follow.objects.all().filter(following=user)
for follower in followers:
streams = stream(post=post, user=follower.follower, date = post.date, following = user)
streams.save()
post_save.connect(stream.add_post, sender=post)
output from print statements
user
<QuerySet []>
[]
I figured it out. It wasn't an issue with the code, but the way that I was creating posts in the admin panel. So because you can only view posts from users that you are following, the posts that I was creating weren't showing up. So I had to create another user, and follow that user, then have the new user post something. Then the post shows up in the page!

How to check if data already exists in the table using django

I am new with django framework struggling to compare value from the database.
this are my tables in models.py :
class Post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE,)
title = models.CharField(max_length=200)
content = models.TextField()
creationDate = models.DateTimeField(auto_now_add=True)
lastEditDate = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
class Votes(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE,)
post_id = models.ForeignKey(Post, on_delete=models.CASCADE,)
up_vote = models.PositiveIntegerField(default=0, validators=[MinValueValidator(0), MaxValueValidator(1)])
down_vote = models.PositiveIntegerField(default=0, validators=[MinValueValidator(0), MaxValueValidator(1)])
class Meta:
unique_together = (("user","post_id"),)
I have data in the vote tabe like this:
Now what I want is to check in the above table if 'user_id' and 'post_id' already exists in the Votes tabel's rows if the exist throw a message if not add value on upvote or downvote, i gues everyone understand what i want if not please let me know.
something which i tried was this code:
def chk_table():
user_id = request.user
post_id = id
votes_table = Votes.objects.filter(user_id=user_id, post_id= post_id).exists()
return votes_table
but this function is checking in hole table not just in just in one row...
Assuming that, in your urls.py
from django.urls import path
from .views import add_vote
urlpatterns = [
path('post/<int:post_id>/vote/add/', add_vote, name='add-vote'),
]
In your views.py
from django.shortcuts import redirect, render
def add_vote(request, post_id):
if request.method == 'POST':
# receive your POST data here
user_id = request.user.id
post_id = post_id
if not Votes.objects.filter(user_id=user_id, post_id=post_id).exists():
Votes.objects.create(**your_data)
redirect('your-desired-url')
else:
# your logic here
I see you already defined unique_together in Meta so you can use try except
from django.db import IntegrityError
try:
# your model create or update code here
except IntegrityError as e:
if 'unique constraint' in e.message:
# duplicate detected

django - relations between models

i have two model which are Post and Profile. i am keepin blog datas which are title,body,owner,slug etc in Post. and keepin' user profile settings which are slogan,email,website etc. in Profile
in my index.html page i display user profile infos and post lists in same page. so ;
i need to connect these two models each other. when someone goes to 127.0.0.1/blog/username (with or without login) all the data which are belong to user named 'username' must be there.
here is my models.py:
class Profile(models.Model):
slogan = models.TextField(blank=True)
twitter = models.CharField(max_length = 100,blank=True)
web_site = models.CharField(max_length=100,blank=True)
email = models.CharField(max_length = 100,blank=True)
def __unicode__(self):
return self.slogan
class Post(models.Model):
owner = models.ForeignKey(User)
title = models.CharField(max_length = 100)
body = models.TextField()
bodyPreview = models.TextField() #preview için body alanı
titlePreview = models.CharField(max_length=100) # preview için title alanı
slug = AutoSlugField(populate_from='title' ,unique=True)
posted = models.DateField(auto_now_add=True)
isdraft = models.BooleanField(default=False)
def __unicode__(self):
return self.title
#permalink
def get_absolute_url(self):
return ('view_blog_post',None,{'postslug':self.slug})
and my index view :
def index(request,username):
post_list = Post.objects.filter(owner__username=username).filter(isdraft=False).order_by("-posted")
p_index = Paginator(post_list,3) #anasayfa için pagination.[her sayfada 3 post]
page = request.GET.get('page')
try:
indexPag = p_index.page(page)
except PageNotAnInteger:
indexPag = p_index.page(1)
except EmptyPage:
indexPag = p_index.page(p_index.num_pages)
## i need to get user's profile datas here. ??
return render_to_response('index.html',
{'post_list':post_list,'p_index':indexPag,'profile':query},
context_instance = RequestContext(request))
I think you should change your Profile model and add a OneToOne relationship to the User model(for more info see here):
class Profile(models.Model):
user = models.OneToOneField(User)
...
class Posts(models.Model):
author = models.ForeignKey(User)
...
and then in your views you can do:
user = get_object_or_404(User, username=username)
post_list = Post.objects.filter(author=user).filter(isdraft=False).order_by("-posted")
return render_to_response('index.html',
{'post_list': post_list, 'user': user, ...}, ...)
And then in your template you are able to access the user's profile.More here
e.g {{user.get_profile.slogan}}
In your view:
def index(request, username):
user = User.objects.get(username=username)
return render(request, 'index.html', {'user':user})
In your template:
{{ user.post_set }}
and You will receive list of posts of current user.
The most natural way to achieve that is to add a OneToOne relation between your Profile model and the django's User model.
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User)
# More fields
class Article(models.Model):
author = models.ForeignKey(User)
# More fields
This way, you can access the profile data through a User object. Would be something like this:
user = User.objecets.get(username=username)
profile = user.profile
More info about this, you can read the django model fields documentation here
and you can also see this cool answer about the diference between the ForeignKey with unique=True and the OneToOneField
I hope this helps