繁体   English   中英

django外键,外键,但无法在管理页面中显示

[英]django foreign key of foreign key but can't show in admin page

我对models.py和views.py有问题。 当用户添加评论评论时,“ Dish_comment_reply_post”始终不显示任何内容,我需要在管理页面中自行选择。 请帮帮我...

models.py:

class Dish_comment(models.Model):
    Dish_comment_post=models.ForeignKey('dish.Dish_post', related_name='comments')
    Your_name=models.CharField(max_length=200)
    Content=models.TextField()
    Dish_comment_created_date=models.DateField(default=timezone.now)
    Dish_comment_approved=models.BooleanField(default=False)

    def approve(self):
        self.Dish_comment_approved=True
        self.save()

    def __str__(self):
        return self.Content

    def Dish_approved_comments(self):
        return self.Dish_comment.filter(Dish_comment_approved=True)

class Dish_comment_reply(models.Model):
    Dish_comment_reply_post=models.ForeignKey('dish.Dish_comment', related_name='replies', null=True)
    Your_name=models.CharField(max_length=200, default='abcd12345')
    Content=models.TextField()
    Dish_comment_reply_created_date=models.DateField(default=timezone.now)

    def __str__(self):
        return self.Content

views.py:

def dish_add_comment_to_post(request,pk):
    dish_post_from_db=get_object_or_404(Dish_post, pk=pk)
    if request.method=="POST":
        dish_comment_form=DishCommentForm(request.POST)
        if dish_comment_form.is_valid():
            dish_comment_from_admin=dish_comment_form.save(commit=False)
            dish_comment_from_admin.Dish_comment_post=dish_post_from_db
            dish_comment_from_admin.save()
            return redirect('dish.views.dish_detail', pk=dish_post_from_db.pk)
    else:
        dish_comment_form=DishCommentForm()
    return render(request, 'dish/dish_add_comment_to_post.html', {'dish_comment_form': dish_comment_form})

@login_required
def dish_comment_approve(request, pk):
    dish_comment_from_db = get_object_or_404(Dish_comment, pk=pk)
    dish_comment_from_db.approve()
    return redirect('dish.views.dish_detail', pk=dish_comment_from_db.Dish_comment_post.pk)

@login_required
def dish_comment_remove(request, pk):
    dish_comment_from_db = get_object_or_404(Dish_comment, pk=pk)
    dish_post_pk = dish_comment_from_db.Dish_comment_post.pk
    dish_comment_from_db.delete()
    return redirect('dish.views.dish_detail', pk=dish_post_pk)

def dish_add_reply_to_comment(request, pk1, pk2):
    dish_post_from_db=get_object_or_404(Dish_post, pk=pk1)
    dish_comment_from_db=get_object_or_404(Dish_comment, pk=pk2)
    if request.method=="POST":
        dish_reply_form=DishCommentReplyForm(request.POST)
        if dish_reply_form.is_valid():
            dish_reply_from_admin=dish_reply_form.save(commit=False)
            dish_reply_from_admin.Dish_comment_reply_post=dish_comment_from_db
            dish_reply_from_admin.save()
            return redirect('dish.views.dish_detail', pk=dish_post_from_db.pk)
    else:
        dish_reply_form=DishCommentReplyForm()
    return render(request, 'dish/dish_add_reply_to_comment.html', {'dish_reply_form': dish_reply_form})

Dish_comment_post=models.ForeignKey('dish.Dish_post', related_name='comments')更改为Dish_comment_post=models.ForeignKey('Dish_post', related_name='comments')Dish_comment_reply_post=models.ForeignKey('dish.Dish_comment', related_name='replies', null=True)Dish_comment_reply_post=models.ForeignKey('Dish_comment', related_name='replies', null=True)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM