0

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 %}

jacky
  • 9
  • 3
  • I think it's worth spending some more time studying the docs. And it seems you create several posts with different usernames? [link1](https://stackoverflow.com/questions/65883626/how-to-query-only-the-pending-interest-and-not-interest-that-is-already-accepted) [link2](https://stackoverflow.com/questions/65879639/how-do-i-include-my-def-clean-slug-function-into-my-views-or-template-so-that-it) ... – Marco Jan 26 '21 at 10:22

1 Answers1

1

Django automatically sets an auto primary field in your BlogPost Model. So you can access it via:

blog_post = get_object_or_404(BlogPost, pk=pk)

And since you have a ForeignKey to the user you can query everything like:

user.blogpost_set.all() # get all blog posts of the user
user.interest_set.all() # get all interests of the user
user.interest_set.filter(
    interest_invite__status="PENDING"
) # get all interest with status pending of the user

Or you can go through User Model like:

User.objects.filter(
    blogpost__interest__interest_invite__status="PENDING"
).distinct() # get all users with interestinvite status PENDING

User.objects.filter(
    blogpost=b, blogpost__interest__interest_invite__status="ACCEPTED"
).distinct() # get all users of a specific BlogPost with status ACCEPTED
Marco
  • 2,371
  • 2
  • 12
  • 19
  • Hi @Marco users can create multiple blog posts. Users can submit only 1 interest to 1 blogpost, and many interests to many blogpost. Just to confirm that in my case, I will replace the word ‘user’ with ‘author’ for the second box and ‘User’ with ‘account’ in the third box right? – jacky Jan 26 '21 at 10:38
  • You can call user whatever you like. It just has to be an instance of `settings.AUTH_USER_MODEL` Model. – Marco Jan 26 '21 at 10:49
  • hi Marco, also would like to check, I understand about the incrementing autofield, but by using pk=pk, will the view be confused between the account.ok and the blogpost.pk @Marco – jacky Jan 26 '21 at 10:57
  • I don't understand. Normally you would not have not user pk and blogpost pk in the URL. Nevertheless, this is perfectly possible. – Marco Jan 26 '21 at 11:16
  • It is not clear why you have this BlogPost pk problem because it's not necessary in your account_view(), so we need more details. – Marco Jan 26 '21 at 11:17
  • So basically I already have user_id in my URL. This helps me to differentiate between diff accounts bro. Eg account/1 account/2 account/3 – jacky Jan 26 '21 at 12:06
  • Now if I were to use pk=Pk, would this conflict with the user_id that is already in the URL? Also, would I receive a name error saying that pk is not defined? So far throughout my project I’ve been using slug=slug instead of pk because I differentiate my blogpost by slug and not by pk @Marco – jacky Jan 26 '21 at 12:08
  • Based on past experience if I use ur code I will get a nameerror saying that pk is not defined. Because previously I used slug=slug and that’s the error I got – jacky Jan 26 '21 at 12:13
  • You can use whatever unique field you like. So you can use `title` or `slug` based on your posted code as well. – Marco Jan 26 '21 at 12:52
  • Hi @Marco, what does blogpost=b mean? –  Jan 26 '21 at 14:26
  • Thanks @tiamgo2, I for got to point it out. `b` is a BlogPost object. – Marco Jan 26 '21 at 16:59