我正在我的应用程序中处理一些Django代码,但显示的是TypeError。
models.py
class Board(models.Model):
name = models.CharField(max_length=30, unique=True)
description = models.CharField(max_length=100)
def __str__(self):
return self.name
class Topic(models.Model):
subject = models.CharField(max_length=255)
last_updated = models.DateTimeField(auto_now_add=True)
board = models.ForeignKey(Board, related_name='topics',on_delete=models.CASCADE)
starter = models.ForeignKey(User, related_name='topics',on_delete=models.CASCADE)
class Post(models.Model):
message = models.TextField(max_length=4000)
topic = models.ForeignKey(Topic, related_name='posts',on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(null=True)
created_by = models.ForeignKey(User, related_name='posts',on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, null=True, related_name='+',on_delete=models.CASCADE)
class Topic(models.Model):
last_updated = models.DateTimeField(auto_now_add=True)
class Post(models.Model):
updated_by = models.ForeignKey(User,null=True,on_delete=models.CASCADE,related_name="+")
views.py
def board_topics(request, pk):
board = get_object_or_404(Board, pk=pk)
return render(request, 'topics.html', {'board': board})
def new_topic(request, pk):
board = get_object_or_404(Board, pk=pk)
if request.method == 'POST':
subject = request.POST.get('subject','')
message = request.POST.get('message','')
user = User.objects.first()
topic = Topic.objects.create(
subject=subject,
board=board,
starter=user
)
post = Post.objects.create(
message=message,
topic=topic,
created_by=user
)
return redirect('board_topics', pk=board.pk)
return render(request, 'new_topic.html', {'board': board})
答案 0 :(得分:1)
删除这些冗余模型
class Topic(models.Model):
last_updated = models.DateTimeField(auto_now_add=True)
class Post(models.Model):
updated_by = models.ForeignKey(User,null=True,on_delete=models.CASCADE,related_name="+")