Manytomany字段数据



我遇到了我的Manustomany问题。我可以在内部添加对象,但是我会遇到麻烦,了解调用数据的请求。这是我的型号.py:

class Parcours(models.Model):
articleList = models.ManyToManyField('Article')
author = models.CharField(max_length=250,blank=True)

class Article(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
fileItem = models.OneToOneField('FileItem', on_delete=models.CASCADE, null=True)
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
thumbnail = models.ImageField(upload_to='media/static')
category = models.ForeignKey('Category',on_delete=models.CASCADE, null=True, blank=True) #TODO
date = models.DateTimeField(default=timezone.now, verbose_name="Date de Parution")
slug = models.SlugField(max_length=100)
content = models.TextField()
adress = models.CharField(max_length=250,blank=True)
autorisation = models.BooleanField(default=True,verbose_name="Autorisation des commentaires")

这是我的Views.py: @login_required def profil(request,id): Myarticle = Article.objects.filter(author=request.user) All = Article.objects.all() UserProfil=User.objects.get(id=id) parkour = Parcours(author=request.user) parkour.save() print(parkour.articleList.all()) return render(request, 'blog/profil.html', locals())

每个人都需要一个作者。我究竟做错了什么?预先感谢。

用于添加和获取ParcoursarticleList您可以做:

parcours = Parcours.objects.create(...)
article = Article.objects.create(...)
parcours.articleList.add(article)
parcours.articleList.all()
<QuerySet [<Article: 1>]>

如果您想获得所有Parcours使用的CC_4:

article.parcours_set.all()
<QuerySet [<Parcours: 1>]>

相关内容

  • 没有找到相关文章

最新更新