简体   繁体   English

django.db.utils.OperationalError:没有这样的列:home_post.assign_to_id

[英]django.db.utils.OperationalError: no such column: home_post.assign_to_id

I want to assign a task to one of the users who commented on the post.我想将任务分配给对帖子发表评论的用户之一。 But when I try to go on post_detail.html an error occurs, and ie但是当我在 post_detail.html 上尝试 go 时,会发生错误,即

OperationalError at /home/ /home/ 处的操作错误

no such column: home_post.assign_to_id没有这样的列:home_post.assign_to_id

PS: I have done python manage.py makemigrations and python manage.py migrate PS:我已经完成了 python manage.py makemigrations 和 python manage.py migrate

So, here is the code snippet.所以,这里是代码片段。 I can give more information if needed.如果需要,我可以提供更多信息。 Thanks in advance!提前致谢!

views.py视图.py

 def SaveAssigned(request):
    if request.method == "POST":
        if request.POST.get('assigned'):
            savevalue = Post()
            savevalue.assign_to = request.POST.get('assigned')
            savevalue.save()
            return render(request, 'post_detail.html')
    else:
        return render(request, 'post_detail.html')

class PostDetailView(DetailView):
    model = Post
    # template_name = 'home/post_detail.html'
    
    def get_context_data(self, *args, **kwargs):
        post_available = get_object_or_404(Post, id=self.kwargs['pk'])
        cat_menu = Category.objects.all()
        context = super(PostDetailView, self).get_context_data()
        context["cat_menu"] = cat_menu
        return context

urls.py网址.py

urlpatterns = [
path('', PostListView.as_view(), name='home'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('post/new/', PostCreateView.as_view(), name='post-create'),
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
path('post/<int:pk>/comment/', AddCommentView.as_view(), name='add-comment'),
path('add_category/', AddCategoryView.as_view(), name='add-category'),
path('category/<str:cats>/', CategoryView, name='category'),
path('category-list/', CategoryListView, name='category-list'),
]

models.py模型.py

class Post(models.Model):
title = models.CharField(max_length = 100)
snippet = models.CharField(max_length= 200)
content = RichTextField(blank=True, null=True)
date_posted = models.DateTimeField(default = timezone.now)
author = models.ForeignKey(User, on_delete= models.CASCADE)
category = models.CharField(max_length=255, default='Coding')
assign_to = models.ForeignKey(User,related_name='assign_to', on_delete=models.CASCADE, null=True)

def __str__(self):
    return self.title + ' | ' + str(self.author)

def get_absolute_url(self):
    return reverse('home')

post_detail.html post_detail.html

{% extends 'users/base.html' %}

{% block body %}
    <article class="media content-section">
      <img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}">
      <div class="media-body">
        <div class="article-metadata">
          <a class="mr-2" href="#">{{ object.author }}</a>
          <small class="text-muted">{{ object.category }}</small>
          <small class="text-muted" style="float: right;">{{ object.date_posted|date:"F d, Y" }}</small>                
        </div>

        <h2 class="article-title">{{ object.title }}</h2>
        <p class="article-content">{{ object.content|safe }}</p>
      </div>
    </article>
    {% if object.author == user %}
      <div>
        <a class="btn btn-outline-secondary bt-sm mt-1 mb-1" href="{% url 'post-update' object.id %}">Update</a>
        <a class="btn btn-outline-danger bt-sm mt-1 mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
        <a class="btn btn-outline-primary bt-sm mt-1 mb-1" style="float: right;" href="">Done</a>
      </div>
      <form method="POST"> 
        {% csrf_token %}
        <select name="assigned">
          <option selected disabled="true"> Select User</option>
          {% if not post.comments.all %}
                    <option>NA</option>
                {% else %}
                    {% for comment in post.comments.all %}
                        <option>{{ comment.author }}</option>
                    {% endfor %}
                {% endif %}
        </select>
        <input type="submit" value="Assign" name="">
      </form>
    {% endif %}
    <br>
    <hr>    
    <h2>Comment Section</h2>
    <br>
    {% if not post.comments.all %}
        No Comments yet
        <br>
        <a href="{% url 'add-comment' post.pk %}">Be the first Ont to Comment!</a>
    {% else %}
        <a href="{% url 'add-comment' post.pk %}">Add a comment</a>
        <br>
        {% for comment in post.comments.all %}
            <strong>
                {{ comment.author }} - {{ comment.date_added }}
            </strong>
            <br>
            {{ comment.body }}
            <br><br>
        {% endfor %}
        <hr>
    {% endif %}
<br><br>
    
{% endblock %}

This error occurs when your model does not contain the specific column and you are trying to add the value in that model with a specific column name.当您的 model 不包含特定列并且您尝试在 model 中添加具有特定列名的值时,会发生此错误。

You can solve this by following steps - Step - 1. python manage.py makemigration Step - 2. python manage.py migrate您可以通过以下步骤解决此问题 - 步骤 - 1. python manage.py makemigration 步骤 - 2. python manage.py migrate

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

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