我制作了一个django博客应用程序,它的工作正常。现在,我正在尝试由用户向博客添加类似功能。
我在我的 Post 模型中添加了一个likes
字段,但是当我运行makemigrations and migrations命令并进入django-admin并单击Post模型时,我得到了这个-没有这样的表格:blogapp_post_likes。
我正在从tutorial中学到东西,他也做了同样的事情,但是他的迁移很成功。
我尝试在执行makemigrations和migration命令之前从项目中删除migrations文件夹,但是它不起作用。
这是我的models.py
文件-
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.contrib.auth.models import User
from datetime import datetime, timedelta
from PIL import Image
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
contact = models.CharField(max_length=12,blank=True)
image = models.ImageField(upload_to='profile_pictures', default='default.png')
description = models.CharField(max_length=100,blank=True,null=True)
def __str__(self):
return f'{self.user.username}'
class Post(models.Model):
author = models.ForeignKey(User,on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
create_date = models.DateTimeField(auto_now_add=True)
published_date = models.DateTimeField(blank=True)
likes = models.ManyToManyField(User,related_name='post_likes',blank=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def approve_comments(self):
return self.comments.filter(approved_comment=True)
def get_absolute_url(self):
return reverse('blogapp:post_detail',kwargs={'pk':self.pk})
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post, related_name='comments', on_delete=models.CASCADE)
author = models.CharField(max_length=100)
text = models.TextField(max_length=264)
created_date = models.DateTimeField(default=timezone.now)
approved_comment = models.BooleanField(default=False)
def approve(self):
self.approved_comment = True
self.save()
def get_absolute_url(self):
return reverse('blogapp:post_list')
def __str__(self):
return self.text
有人可以帮忙吗?