我有POST请求中的字符串列表,我想创建博客对象 具有很多标签的值,通过这种方法,它仅创建标签 具有1个值的对象,然后将其覆盖,该怎么办?
def myForm(request):
tags = HashTags.objects.all()
if request.method == 'POST':
g = request.POST
print(g)
if g.get('title') and g.get('desc') and request.FILES.get('file'):
obj = Blog.objects.create(
title = g.get('title'),
text = g.get('desc'),
photo1 = request.FILES.get('file'))
if g.get('VIN'):
obj.Vin = g.get('VIN')
if g.getlist('tags'):
for i in g.getlist('tags'):
print(i)
a = HashTags.objects.filter(tag=i)
obj.hashtag.tag = g.getlist('tags')[0]
return redirect('blog:main')
class HashTags(models.Model):
tag = models.CharField(max_length=150,unique=True) #unique=True
def __str__(self):
return self.tag
class Blog(models.Model):
title = models.CharField(max_length=150)
date = models.DateTimeField(auto_now_add=True)
text = models.TextField()
photo1 = models.ImageField()
photo2 = models.ImageField(null=True,blank=True)
videoURL = models.CharField(max_length = 5000)
hashtag = models.ManyToManyField(HashTags)
slug = models.SlugField(null=True,blank=True)
pinVideo =models.BooleanField(null=True,blank=True,default=False)
pinPhoto = models.BooleanField(null=True,blank=True,default=False)
实际:randomtag2
预期:randomtag1,randomtag2
答案 0 :(得分:0)
您可以将add用于许多字段
max-age