Sorry I am quite new to Django.
Ok in my project, every blogpost has their own group of members. You are only a member if your interest status is 'Accepted', and you get auto added to the member list. Every user can "submit interest" to any blogpost.
So now on the account page of the user, i want to query the blog posts that the user is a member to (aka interest status = "accepted")
I also want to query ALL the blog posts that the user has submitted interest to and that they are waiting to be accepted. to (aka interest status = "pending" and not a member yet)
In case you are confused, the members is a field of BlogPost model, and the status is a field in the InterestInvite model :).....
so in my template i want to have the title of ALL those blogpost that I am a member to and those blogpost where my interest is still pending status to be displayed. And when I click on the title, I will be directed to that particular blog post. Can anyone share how this querying can be done?
Problem i am facing is that i cannot use blog_post = get_object_or_404(BlogPost, slug=slug)
in my views.py because they will say NameError because slug not defined. And I have no foreign key in my Account model to the BlogPost model. My foreign key is only in the BlogPost model.
Not sure if anyone has any solution to the querying and how I can display the info on the template. Thank you!
models.py
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name="email", max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
class BlogPost(models.Model):
title = models.CharField(max_length=50, null=False, blank=False, unique=True)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
slug = models.SlugField(blank=True, unique=True)
members = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name="members")
class Interest(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
blog_post = models.ForeignKey(BlogPost, on_delete=models.CASCADE)
class InterestInvite(models.Model):
ACCEPT = "ACCEPT"
DECLINE = "DECLINE"
PENDING = "PENDING"
STATUS_CHOICES = [
(ACCEPT, "accept"),
(DECLINE, "decline"),
(PENDING, "pending"),
]
interest = models.OneToOneField(Interest, on_delete=models.CASCADE, related_name="interest_invite")
status = models.CharField(max_length=25, choices=STATUS_CHOICES, default=PENDING)
urls.py
path('<user_id>/', account_view, name="view"),
views.py
def account_view(request, *args, **kwargs):
context = {}
user_id = kwargs.get("user_id")
try:
account = Account.objects.get(pk=user_id)
context['account'] = account
except:
return HttpResponse("Something went wrong.")
if account:
context['id'] = account.id
context['username'] = account.username
context['email'] = account.email
context['profile_image'] = account.profile_image.url
context['hide_email'] = account.hide_email
context['biography'] = account.biography
blog_posts = BlogPost.objects.filter(author=account)
context['blog_posts'] = blog_posts
return render(request, "account/account.html", context)
account.html
{% if request.user in blog_post.members.all %}
{% endif %}
question from:
https://stackoverflow.com/questions/65898144/how-to-query-the-blogpost-wihtout-slug-as-my-function-argument