多对多字段返回无 django
Posted
技术标签:
【中文标题】多对多字段返回无 django【英文标题】:Many to Many field Returning None django 【发布时间】:2018-12-29 08:43:09 【问题描述】:当我尝试在终端中打印一个字段(多对多)时,我只得到“无”的结果。我错过了什么吗?
view.py
def assign_skill (request, pk):
plan = get_object_or_404(Plan, pk=pk)
if(request.GET.get('assign_skill')):
print("nummer1")
print(plan.title)
print(plan.skillonplan)
return redirect('all-plan')
models.py
class Plan(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, editable=False)
subscriber = models.ManyToManyField(User, blank = True, related_name = 'plansubscriber')
title = models.CharField(max_length=100, default = '', null=True, blank=True)
description = models.TextField(max_length=10000, default = '', null=True, blank=True)
company = models.CharField(max_length=200, null=True, editable=False)
created_date = models.DateTimeField(default=timezone.now)
skillonplan = models.ManyToManyField(Skill, null=True, blank = True, related_name='planneke')
duration = models.IntegerField(default=0,
validators=[MaxValueValidator(36), MinValueValidator(1)]
)
def get_skills(self):
return '\n'.join([p.skills for p in self.skill.all()])
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse_lazy('plan_detail', kwargs='pk': self.id)
【问题讨论】:
【参考方案1】:您可以使用以下代码,以返回多对多字段:
print(plan.skillonplan.all())
【讨论】:
以上是关于多对多字段返回无 django的主要内容,如果未能解决你的问题,请参考以下文章