关系“myApp_post_share”不存在第 1 行:...LECT“auth_user”.“id” FROM “auth_user” INNER JOIN “myApp_pos”

时间:2021-01-27 14:09:54

标签: django-models many-to-many

我希望用户能够通过用户名向其他人发送文件。 但是问题又来了。我该如何解决?

shared_user_id 是我们想要分享的用户的 id

models.py

  class Post(models.Model):
        author = models.ForeignKey(User, on_delete=models.CASCADE, unique=True)
        desc = models.TextField()
        file_field = models.FileField(upload_to=image_directory_path, storage=image_storage)
        date_posted = models.DateTimeField(default=timezone.now)
        share = models.ManyToManyField(User, blank=True, null=True,
                                       related_name="SharedUsers")
    
        def __str__(self):
            return f'{self.author}=> {self.desc}'
    
        def get_absolute_url(self):
            return reverse('post-detail', kwargs={'pk':self.pk})
    
        @property
        def expired_date(self):
            "Returns the expiration date of the File."
            return self.date_posted + datetime.timedelta(days=7)

views.py

def file_share(request, pk):
    form = FileShareForm(request.POST or None)
    post = Post.objects.get(id=pk)

    if request.user.is_authenticated and post.author == request.user:
        if form.is_valid():
            shared_user_id = request.POST.get('share')
            post.share.add(*shared_user_id)
    context = {
        'form': form,
    }
    template = 'myApp/file_share.html'**strong text**
    return render(request, template, context)

错误:

The above exception (relation "myApp_post_share" does not exist LINE 1: ...LECT "auth_user"."id" FROM "auth_user" INNER JOIN "myApp_pos... ^ ) was the direct cause of the following exception:

0 个答案:

没有答案