新帖子中的HashTag与数据库中的每个其他帖子相关联。我需要它们才能连接到具有相同主题标签的帖子,以便在之后实现主题标签云。 出现问题,当我添加新帖子并制作新标签甚至更新旧标签时,如果我转到管理部分并检查,标签会连接到数据库中的所有帖子。
我做错了什么。暂时坚持了一段时间。
这是我的代码:
我的models.py
from django.db import models
# Create your models here.
from user_profile.models import User
class Post(models.Model):
# postad_category = models.ForeignKey(Category)
# postad_name = models.CharField(max_length=250)
user = models.ForeignKey(User)
text = models.CharField(max_length=300)
created_date = models.DateTimeField(auto_now=True)
country = models.CharField(max_length=30, default="Global")
# postad_logo = models.CharField(max_length=1000)
is_active = models.BooleanField(default=True)
is_favorite = models.BooleanField(default=False)
def __str__(self):
return self.name
class HashTag(models.Model):
"""HashTag model"""
name = models.CharField(max_length=64, unique=True)
post = models.ManyToManyField(Post)
def __str__(self):
return self.name
我的views.py
class PostPost(View):
"""Post - post form available on page /user/<username> URL"""
def post(self, request, username):
form = PostForm(self.request.POST)
if form.is_valid():
# post = form.save(commit = False)
# post.user = request.user
user = User.objects.get(username=username)
post = Post(text=form.cleaned_data['text'],
user=user,
# country=form.cleaned_data['country']
)
post.save()
words = form.cleaned_data['text'].split(" ")
for word in words:
if word[0] == "#":
hashtag, created = HashTag.objects.get_or_create(name=word[1:])
print(hashtag,'\n', created)
hashtag.post.add(post)
return HttpResponseRedirect('/user/'+username)
答案 0 :(得分:1)
from django.db import models
from user_profile.models import User
class Post(models.Model):
# postad_category = models.ForeignKey(Category)
# postad_name = models.CharField(max_length=250)
user = models.ForeignKey(User)
text = models.CharField(max_length=300)
created_date = models.DateTimeField(auto_now=True)
country = models.CharField(max_length=30, default="Global")
# postad_logo = models.CharField(max_length=1000)
is_active = models.BooleanField(default=True)
is_favorite = models.BooleanField(default=False)
def __str__(self):
# This should be text, not name.
return self.text
class HashTag(models.Model):
"""HashTag model"""
name = models.CharField(max_length=64, unique=True)
post = models.ManyToManyField(Post)
def __str__(self):
return self.name
# Sometimes get or create dosnt work properly
from django.shortcuts import render
from django.core.exceptions import ObjectDoesNotExist
class PostPost(View):
"""Post - post form available on page /user/<username> URL"""
def post(self, request, username):
form = PostForm(self.request.POST)
if form.is_valid():
# post = form.save(commit = False)
# post.user = request.user
user = User.objects.get(username=username)
post = Post(text=form.cleaned_data['text'],
user=user,
# country=form.cleaned_data['country']
)
post.save()
words = form.cleaned_data['text'].split(" ")
for word in words:
if word[0] == "#":
try:
hashtag = HashTag.objects.get(name=word[1:])
created = False
except ObjectDoesNotExist:
hashtag, created= HashTag.objects.create(name=word[1:])
print(hashtag,'\n', created)
hashtag.post.add(post)
return HttpResponseRedirect('/user/'+username)
答案 1 :(得分:0)
所以,谢谢大家。但我尝试了很多东西,直到我得到一个新版本的django并且没有再使用相同的代码它没有帮助。我认为问题出在django的版本中。