Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

django - How to query the blogpost wihtout slug as my function argument?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...